From 7ee6043dba983bd30bdb880a216cb573d76c7fc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Marczewski?= Date: Tue, 26 May 2015 21:38:01 +0200 Subject: [PATCH] Fixed #24847 -- Prevented items set on a RequestContext from being lost. Backport of 300e8baf9429dbf1c5dff478558d793638dc2f54 from master --- django/template/context.py | 8 +++++++- docs/releases/1.8.3.txt | 3 +++ tests/template_tests/test_context.py | 11 +++++++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/django/template/context.py b/django/template/context.py index af4fb8853b..e77866cc5d 100644 --- a/django/template/context.py +++ b/django/template/context.py @@ -219,7 +219,13 @@ class RequestContext(Context): self.request = request self._processors = () if processors is None else tuple(processors) self._processors_index = len(self.dicts) - self.update({}) # placeholder for context processors output + + # placeholder for context processors output + self.update({}) + + # empty dict for any new modifications + # (so that context processors don't overwrite them) + self.update({}) @contextmanager def bind_template(self, template): diff --git a/docs/releases/1.8.3.txt b/docs/releases/1.8.3.txt index d965630c77..5adea05dd1 100644 --- a/docs/releases/1.8.3.txt +++ b/docs/releases/1.8.3.txt @@ -22,3 +22,6 @@ Bugfixes * Reverted an optimization to the CSRF template context processor which caused a regression (:ticket:`24836`). + +* Fixed a regression which caused template context processors to overwrite + variables set on a ``RequestContext`` after it's created (:ticket:`24847`). diff --git a/tests/template_tests/test_context.py b/tests/template_tests/test_context.py index 8331904f64..8b340b402d 100644 --- a/tests/template_tests/test_context.py +++ b/tests/template_tests/test_context.py @@ -134,8 +134,8 @@ class RequestContextTests(SimpleTestCase): request = RequestFactory().get('/') ctx = RequestContext(request, {}) # The stack should now contain 3 items: - # [builtins, supplied context, context processor] - self.assertEqual(len(ctx.dicts), 3) + # [builtins, supplied context, context processor, empty dict] + self.assertEqual(len(ctx.dicts), 4) def test_context_comparable(self): # Create an engine without any context processors. @@ -149,3 +149,10 @@ class RequestContextTests(SimpleTestCase): RequestContext(request, dict_=test_data), RequestContext(request, dict_=test_data), ) + + def test_modify_context_and_render(self): + template = Template('{{ foo }}') + request = RequestFactory().get('/') + context = RequestContext(request, {}) + context['foo'] = 'foo' + self.assertEqual(template.render(context), 'foo')