Loading django/template/context.py +1 −2 Original line number Diff line number Diff line Loading @@ -171,8 +171,7 @@ class Context(BaseContext): "Pushes other_dict to the stack of dictionaries in the Context" if not hasattr(other_dict, '__getitem__'): raise TypeError('other_dict must be a mapping (dictionary-like) object.') self.dicts.append(other_dict) return other_dict return ContextDict(self, other_dict) class RenderContext(BaseContext): Loading docs/ref/templates/api.txt +15 −0 Original line number Diff line number Diff line Loading @@ -497,6 +497,21 @@ the stack instead of an empty one. >>> c['foo'] 'first level' Like ``push()``, you can use ``update()`` as a context manager to ensure a matching ``pop()`` is called. >>> c = Context() >>> c['foo'] = 'first level' >>> with c.update({'foo': 'second level'}): ... c['foo'] 'second level' >>> c['foo'] 'first level' .. versionadded:: 1.9 The ability to use ``update()`` as a context manager was added. Using a ``Context`` as a stack comes in handy in :ref:`some custom template tags <howto-writing-custom-template-tags>`. Loading docs/releases/1.9.txt +3 −0 Original line number Diff line number Diff line Loading @@ -211,6 +211,9 @@ Templates * The :ttag:`firstof` template tag supports storing the output in a variable using 'as'. * :meth:`Context.update() <django.template.Context.update>` can now be used as a context manager. Requests and Responses ^^^^^^^^^^^^^^^^^^^^^^ Loading tests/template_tests/test_context.py +13 −0 Original line number Diff line number Diff line Loading @@ -21,6 +21,8 @@ class ContextTests(SimpleTestCase): self.assertEqual(c["a"], 1) self.assertEqual(c.get("foo", 42), 42) def test_push_context_manager(self): c = Context({"a": 1}) with c.push(): c['a'] = 2 self.assertEqual(c['a'], 2) Loading @@ -30,6 +32,17 @@ class ContextTests(SimpleTestCase): self.assertEqual(c['a'], 3) self.assertEqual(c['a'], 1) def test_update_context_manager(self): c = Context({"a": 1}) with c.update({}): c['a'] = 2 self.assertEqual(c['a'], 2) self.assertEqual(c['a'], 1) with c.update({'a': 3}): self.assertEqual(c['a'], 3) self.assertEqual(c['a'], 1) def test_setdefault(self): c = Context() Loading Loading
django/template/context.py +1 −2 Original line number Diff line number Diff line Loading @@ -171,8 +171,7 @@ class Context(BaseContext): "Pushes other_dict to the stack of dictionaries in the Context" if not hasattr(other_dict, '__getitem__'): raise TypeError('other_dict must be a mapping (dictionary-like) object.') self.dicts.append(other_dict) return other_dict return ContextDict(self, other_dict) class RenderContext(BaseContext): Loading
docs/ref/templates/api.txt +15 −0 Original line number Diff line number Diff line Loading @@ -497,6 +497,21 @@ the stack instead of an empty one. >>> c['foo'] 'first level' Like ``push()``, you can use ``update()`` as a context manager to ensure a matching ``pop()`` is called. >>> c = Context() >>> c['foo'] = 'first level' >>> with c.update({'foo': 'second level'}): ... c['foo'] 'second level' >>> c['foo'] 'first level' .. versionadded:: 1.9 The ability to use ``update()`` as a context manager was added. Using a ``Context`` as a stack comes in handy in :ref:`some custom template tags <howto-writing-custom-template-tags>`. Loading
docs/releases/1.9.txt +3 −0 Original line number Diff line number Diff line Loading @@ -211,6 +211,9 @@ Templates * The :ttag:`firstof` template tag supports storing the output in a variable using 'as'. * :meth:`Context.update() <django.template.Context.update>` can now be used as a context manager. Requests and Responses ^^^^^^^^^^^^^^^^^^^^^^ Loading
tests/template_tests/test_context.py +13 −0 Original line number Diff line number Diff line Loading @@ -21,6 +21,8 @@ class ContextTests(SimpleTestCase): self.assertEqual(c["a"], 1) self.assertEqual(c.get("foo", 42), 42) def test_push_context_manager(self): c = Context({"a": 1}) with c.push(): c['a'] = 2 self.assertEqual(c['a'], 2) Loading @@ -30,6 +32,17 @@ class ContextTests(SimpleTestCase): self.assertEqual(c['a'], 3) self.assertEqual(c['a'], 1) def test_update_context_manager(self): c = Context({"a": 1}) with c.update({}): c['a'] = 2 self.assertEqual(c['a'], 2) self.assertEqual(c['a'], 1) with c.update({'a': 3}): self.assertEqual(c['a'], 3) self.assertEqual(c['a'], 1) def test_setdefault(self): c = Context() Loading