Ticket #13841: 13841_tests_docs.diff
File 13841_tests_docs.diff, 4.7 KB (added by , 13 years ago) |
---|
-
django/template/context.py
diff --git a/django/template/context.py b/django/template/context.py index 8f70d70..383e184 100644
a b 1 1 from copy import copy 2 from inspect import getargspec 2 3 from django.core.exceptions import ImproperlyConfigured 3 4 from django.utils.importlib import import_module 4 5 … … class RequestContext(Context): 169 170 else: 170 171 processors = tuple(processors) 171 172 for processor in get_standard_processors() + processors: 172 self.update(processor(request)) 173 if not hasattr(processor, 'pass_context'): 174 arginfo = getargspec(processor) 175 processor.pass_context = len(arginfo[0]) > 1 176 if processor.pass_context: 177 self.update(processor(request, context=self)) 178 else: 179 self.update(processor(request)) -
docs/ref/templates/api.txt
diff --git a/docs/ref/templates/api.txt b/docs/ref/templates/api.txt index c8100fc..db19756 100644
a b Writing your own context processors 526 526 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 527 527 528 528 A context processor has a very simple interface: It's just a Python function 529 that takes one argument, an :class:`~django.http.HttpRequest` object, and 530 returns a dictionary that gets added to the template context. Each context 531 processor *must* return a dictionary. 529 that takes at least one argument, an :class:`~django.http.HttpRequest` object. 530 531 .. versionadded:: 1.2 532 The optional keyword argument ``context`` is now supported, which allows 533 access to the current :class:`~django.template.Context` object. 534 535 The function returns a dictionary that gets added to the template context. 536 Each context processor *must* return a dictionary. 532 537 533 538 Custom context processors can live anywhere in your code base. All Django cares 534 539 about is that your custom context processors are pointed-to by your -
tests/regressiontests/context_processors/tests.py
diff --git a/tests/regressiontests/context_processors/tests.py b/tests/regressiontests/context_processors/tests.py index 1f2209e..53eefca 100644
a b 2 2 Tests for Django's bundled context processors. 3 3 """ 4 4 from django.test import TestCase 5 from django.template import RequestContext, Template 6 from django.test.client import RequestFactory 5 7 6 8 7 9 class RequestContextProcessorTests(TestCase): … … class RequestContextProcessorTests(TestCase): 11 13 12 14 urls = 'regressiontests.context_processors.urls' 13 15 16 def _render(self, template, context, processors=None): 17 """ 18 Helper function to ease testing of context processors without 19 needing templates on the file system 20 """ 21 factory = RequestFactory() 22 request = factory.get('/request_attrs/') 23 context = RequestContext(request, context, processors=processors) 24 return Template(template).render(context) 25 14 26 def test_request_attributes(self): 15 27 """ 16 28 Test that the request object is available in the template and that its … … class RequestContextProcessorTests(TestCase): 35 47 response = self.client.post(url, {'path': '/blah/'}) 36 48 self.assertContains(response, url) 37 49 50 def test_no_passed_context(self): 51 """ 52 Context processors not expecting the "context" argument should 53 continue to work as usual (refs #13841) 54 """ 55 def add_vegetable(request): 56 return {'vegetable': 'broccoli'} 57 # vegetable shuldn't exist yet in the context 58 content = self._render('{{ fruit }} {{ vegetable }}', 59 {'fruit': 'apple'}) 60 self.assertEqual(content, 'apple ') 61 # add_vegetable context processor should add "broccoli" to context 62 content = self._render('{{ fruit }} {{ vegetable }}', 63 {'fruit': 'apple'}, [add_vegetable]) 64 self.assertEqual(content, 'apple broccoli') 65 66 def test_can_access_context(self): 67 """ 68 Test context processor expecting the "context" argument can access 69 and modify the context (refs #13841) 70 """ 71 def pluralize_fruit(request, context): 72 return {'fruit': context['fruit'] + 's'} 73 # should just print apple (default behavior) 74 content = self._render('{{ fruit }}', {'fruit': 'apple'}) 75 self.assertEqual(content, 'apple') 76 # the pluralize_fruit context processor should add an 's' to apple 77 content = self._render('{{ fruit }}', {'fruit': 'apple'}, 78 [pluralize_fruit]) 79 self.assertEqual(content, 'apples')