Fixed #24847 -- Prevented items set on a RequestContext from being lost.

This commit is contained in:
Paweł Marczewski 2015-05-26 21:38:01 +02:00 committed by Tim Graham
parent 70be31bba7
commit 300e8baf94
3 changed files with 20 additions and 4 deletions

View File

@ -226,7 +226,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):

View File

@ -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`).

View File

@ -2,7 +2,7 @@
from django.http import HttpRequest
from django.template import (
Context, Engine, RequestContext, Variable, VariableDoesNotExist,
Context, Engine, RequestContext, Template, Variable, VariableDoesNotExist,
)
from django.template.context import RenderContext
from django.test import RequestFactory, SimpleTestCase
@ -153,8 +153,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.
@ -168,3 +168,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')