Author: SmileyChris Date: 2011-04-16 21:52:31 -0700 (Sat, 16 Apr 2011) New Revision: 16031
Modified: django/trunk/django/template/context.py django/trunk/tests/regressiontests/templates/tests.py Log: Fixes #15721 -- Make {% include %} and RequestContext work together again. Modified: django/trunk/django/template/context.py =================================================================== --- django/trunk/django/template/context.py 2011-04-17 04:52:17 UTC (rev 16030) +++ django/trunk/django/template/context.py 2011-04-17 04:52:31 UTC (rev 16031) @@ -14,21 +14,16 @@ "pop() has been called more times than push()" pass -class EmptyClass(object): - # No-op class which takes no args to its __init__ method, to help implement - # __copy__ - pass - class BaseContext(object): def __init__(self, dict_=None): - dict_ = dict_ or {} - self.dicts = [dict_] + self._reset_dicts(dict_) + def _reset_dicts(self, value=None): + self.dicts = [value or {}] + def __copy__(self): - duplicate = EmptyClass() - duplicate.__class__ = self.__class__ - duplicate.__dict__ = self.__dict__.copy() - duplicate.dicts = duplicate.dicts[:] + duplicate = copy(super(BaseContext, self)) + duplicate.dicts = self.dicts[:] return duplicate def __repr__(self): @@ -78,6 +73,15 @@ return d[key] return otherwise + def new(self, values=None): + """ + Returns a new context with the same properties, but with only the + values given in 'values' stored. + """ + new_context = copy(self) + new_context._reset_dicts(values) + return new_context + class Context(BaseContext): "A stack container for variable context" def __init__(self, dict_=None, autoescape=True, current_app=None, use_l10n=None): @@ -88,7 +92,7 @@ super(Context, self).__init__(dict_) def __copy__(self): - duplicate = super(Context, self).__copy__() + duplicate = copy(super(Context, self)) duplicate.render_context = copy(self.render_context) return duplicate @@ -99,14 +103,6 @@ self.dicts.append(other_dict) return other_dict - def new(self, values=None): - """ - Returns a new Context with the same 'autoescape' value etc, but with - only the values given in 'values' stored. - """ - return self.__class__(dict_=values, autoescape=self.autoescape, - current_app=self.current_app, use_l10n=self.use_l10n) - class RenderContext(BaseContext): """ A stack container for storing Template state. Modified: django/trunk/tests/regressiontests/templates/tests.py =================================================================== --- django/trunk/tests/regressiontests/templates/tests.py 2011-04-17 04:52:17 UTC (rev 16030) +++ django/trunk/tests/regressiontests/templates/tests.py 2011-04-17 04:52:31 UTC (rev 16031) @@ -1639,5 +1639,34 @@ settings.INSTALLED_APPS = ('tagsegg',) t = template.Template(ttext) + +class RequestContextTests(BaseTemplateResponseTest): + + def setUp(self): + templates = { + 'child': Template('{{ var|default:"none" }}'), + } + setup_test_template_loader(templates) + self.fake_request = RequestFactory().get('/') + + def tearDown(self): + restore_template_loaders() + + def test_include_only(self): + """ + Regression test for #15721, ``{% include %}`` and ``RequestContext`` + not playing together nicely. + """ + ctx = RequestContext(self.fake_request, {'var': 'parent'}) + self.assertEqual( + template.Template('{% include "child" %}').render(ctx), + 'parent' + ) + self.assertEqual( + template.Template('{% include "child" only %}').render(ctx), + 'none' + ) + + if __name__ == "__main__": unittest.main() -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.