From 4c519867904918d8cdf2ded91af3e87bd1c02664 Mon Sep 17 00:00:00 2001 From: Chris Beaven Date: Wed, 24 Nov 2010 00:36:36 +0000 Subject: [PATCH] Fixes #3529 -- more explicit documentation about Context.update. Thanks for the patch, ggetzie. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14689 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/template/context.py | 2 +- docs/ref/templates/api.txt | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/django/template/context.py b/django/template/context.py index a5b417cdd3..de7c39f17b 100644 --- a/django/template/context.py +++ b/django/template/context.py @@ -74,7 +74,7 @@ class Context(BaseContext): super(Context, self).__init__(dict_) def update(self, other_dict): - "Like dict.update(). Pushes an entire dictionary's keys and values onto the context." + "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) diff --git a/docs/ref/templates/api.txt b/docs/ref/templates/api.txt index e02debe39b..44de4d9906 100644 --- a/docs/ref/templates/api.txt +++ b/docs/ref/templates/api.txt @@ -281,6 +281,22 @@ If you ``pop()`` too much, it'll raise ... django.template.ContextPopException +In addition to ``push()`` and ``pop()``, the ``Context`` +object also defines an ``update()`` method. This works like ``push()`` +but takes a dictionary as an argument and pushes that dictionary onto +the stack instead of an empty one. + + >>> c = Context() + >>> c['foo'] = 'first level' + >>> c.update({'foo': 'updated'}) + {'foo': 'updated'} + >>> c['foo'] + 'updated' + >>> c.pop() + {'foo': 'updated'} + >>> c['foo'] + 'first level' + Using a ``Context`` as a stack comes in handy in some custom template tags, as you'll see below.