summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2015-01-06 20:56:54 +0100
committerAymeric Augustin <aymeric.augustin@m4x.org>2015-01-06 22:02:27 +0100
commit0cdb09d48944df3f13cc1a4e1f5c49b786174ef9 (patch)
tree6fa089d32660c286d06f0ffaf1ad9de28e4893ac
parented220c4cbea04c2e77535af93bcfaa917cce607a (diff)
Made context take priority over context processors.
This is the expected behavior, but given RequestContext's tortuous implementation, a straightforward use of its API results in the opposite. This commits fixes a regression that must have happened at different points in the multiple templates engine refactor for different features.
-rw-r--r--django/template/backends/django.py8
-rw-r--r--tests/template_backends/test_django.py25
2 files changed, 32 insertions, 1 deletions
diff --git a/django/template/backends/django.py b/django/template/backends/django.py
index 580d789a67..63747f7c7f 100644
--- a/django/template/backends/django.py
+++ b/django/template/backends/django.py
@@ -45,5 +45,11 @@ class Template(object):
if request is None:
context = Context(context)
else:
- context = RequestContext(request, context)
+ # The following pattern is required to ensure values from
+ # context override those from template context processors.
+ original_context = context
+ context = RequestContext(request)
+ if original_context:
+ context.push(original_context)
+
return self.template.render(context)
diff --git a/tests/template_backends/test_django.py b/tests/template_backends/test_django.py
index 5c5070cb18..f332abbf24 100644
--- a/tests/template_backends/test_django.py
+++ b/tests/template_backends/test_django.py
@@ -1,4 +1,7 @@
from django.template.backends.django import DjangoTemplates
+from django.test import RequestFactory
+
+from template_tests.test_response import test_processor_name
from .test_dummy import TemplateStringsTests
@@ -7,3 +10,25 @@ class DjangoTemplatesTests(TemplateStringsTests):
engine_class = DjangoTemplates
backend_name = 'django'
+
+ def test_context_has_priority_over_template_context_processors(self):
+ # See ticket #23789.
+ engine = DjangoTemplates({
+ 'DIRS': [],
+ 'APP_DIRS': False,
+ 'NAME': 'django',
+ 'OPTIONS': {
+ 'context_processors': [test_processor_name],
+ },
+ })
+
+ template = engine.from_string('{{ processors }}')
+ request = RequestFactory().get('/')
+
+ # Check that context processors run
+ content = template.render({}, request)
+ self.assertEqual(content, 'yes')
+
+ # Check that context overrides context processors
+ content = template.render({'processors': 'no'}, request)
+ self.assertEqual(content, 'no')