mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
Allowed Context.push to behave as a context mananger.
Thanks Loic Bistuer for the review.
This commit is contained in:
committed by
Tim Graham
parent
828359e52d
commit
a3e7d73ed7
@@ -325,6 +325,31 @@ If you ``pop()`` too much, it'll raise
|
||||
...
|
||||
django.template.ContextPopException
|
||||
|
||||
.. versionadded:: 1.7
|
||||
|
||||
You can also use ``push()`` as a context manager to ensure a matching ``pop()``
|
||||
is called.
|
||||
|
||||
>>> c = Context()
|
||||
>>> c['foo'] = 'first level'
|
||||
>>> with c.push():
|
||||
>>> c['foo'] = 'second level'
|
||||
>>> c['foo']
|
||||
'second level'
|
||||
>>> c['foo']
|
||||
'first level'
|
||||
|
||||
All arguments passed to ``push()`` will be passed to the ``dict`` constructor
|
||||
used to build the new context level.
|
||||
|
||||
>>> c = Context()
|
||||
>>> c['foo'] = 'first level'
|
||||
>>> with c.push(foo='second level'):
|
||||
>>> c['foo']
|
||||
'second level'
|
||||
>>> c['foo']
|
||||
'first level'
|
||||
|
||||
.. method:: update(other_dict)
|
||||
|
||||
In addition to ``push()`` and ``pop()``, the ``Context``
|
||||
|
||||
Reference in New Issue
Block a user