From 93240b7d903af86e135c3b1724a81d2daf31441f Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Tue, 10 Apr 2012 20:49:45 +0000 Subject: [PATCH] Fixed #17229 -- Allow 'True', 'False' and 'None' to resolve to the corresponding Python objects in templates. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17894 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/template/context.py | 5 ++++- docs/ref/templates/api.txt | 14 +++++++++++++- docs/releases/1.5.txt | 8 ++++++++ docs/topics/i18n/timezones.txt | 6 ------ tests/regressiontests/templates/tests.py | 9 +++++---- 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/django/template/context.py b/django/template/context.py index bbd38ad468..8a5f0d3c54 100644 --- a/django/template/context.py +++ b/django/template/context.py @@ -18,7 +18,10 @@ class BaseContext(object): self._reset_dicts(dict_) def _reset_dicts(self, value=None): - self.dicts = [value or {}] + builtins = {'True': True, 'False': False, 'None': None} + if value: + builtins.update(value) + self.dicts = [builtins] def __copy__(self): duplicate = copy(super(BaseContext, self)) diff --git a/docs/ref/templates/api.txt b/docs/ref/templates/api.txt index 03fef1da45..605f0a5698 100644 --- a/docs/ref/templates/api.txt +++ b/docs/ref/templates/api.txt @@ -111,6 +111,9 @@ template:: >>> t.render(c) "My name is Dolores." +Variables and lookups +~~~~~~~~~~~~~~~~~~~~~ + Variable names must consist of any letter (A-Z), any digit (0-9), an underscore (but they must not start with an underscore) or a dot. @@ -225,7 +228,6 @@ straight lookups. Here are some things to keep in mind: if your variable is not callable (allowing you to access attributes of the callable, for example). - .. _invalid-template-variables: How invalid variables are handled @@ -263,6 +265,16 @@ be replaced with the name of the invalid variable. in order to debug a specific template problem, then cleared once debugging is complete. +Builtin variables +~~~~~~~~~~~~~~~~~ + +Every context contains ``True``, ``False`` and ``None``. As you would expect, +these variables resolve to the corresponding Python objects. + +.. versionadded:: 1.5 + Before Django 1.5, these variables weren't a special case, and they + resolved to ``None`` unless you defined them in the context. + Playing with Context objects ---------------------------- diff --git a/docs/releases/1.5.txt b/docs/releases/1.5.txt index 84459f9021..91a61648df 100644 --- a/docs/releases/1.5.txt +++ b/docs/releases/1.5.txt @@ -33,6 +33,14 @@ Django 1.5 does not run on Jython. What's new in Django 1.5 ======================== +Minor features +~~~~~~~~~~~~~~ + +Django 1.5 also includes several smaller improvements worth noting: + +* The template engine now interprets ``True``, ``False`` and ``None`` as the + corresponding Python objects. + Backwards incompatible changes in 1.5 ===================================== diff --git a/docs/topics/i18n/timezones.txt b/docs/topics/i18n/timezones.txt index e5453ef960..093f120644 100644 --- a/docs/topics/i18n/timezones.txt +++ b/docs/topics/i18n/timezones.txt @@ -307,12 +307,6 @@ time zone is unset, the default time zone applies. Server time: {{ value }} {% endtimezone %} -.. note:: - - In the second block, ``None`` resolves to the Python object ``None`` - because it isn't defined in the template context, not because it's the - string ``None``. - .. templatetag:: get_current_timezone get_current_timezone diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index e40ffcbdc4..3f230f8c13 100644 --- a/tests/regressiontests/templates/tests.py +++ b/tests/regressiontests/templates/tests.py @@ -372,13 +372,11 @@ class Templates(unittest.TestCase): with self.assertRaises(urlresolvers.NoReverseMatch): t.render(c) - - @override_settings(DEBUG=True, TEMPLATE_DEBUG = True) + @override_settings(DEBUG=True, TEMPLATE_DEBUG=True) def test_no_wrapped_exception(self): """ The template system doesn't wrap exceptions, but annotates them. Refs #16770 - """ c = Context({"coconuts": lambda: 42 / 0}) t = Template("{{ coconuts }}") @@ -387,7 +385,6 @@ class Templates(unittest.TestCase): self.assertEqual(cm.exception.django_template_source[1], (0, 14)) - def test_invalid_block_suggestion(self): # See #7876 from django.template import Template, TemplateSyntaxError @@ -610,6 +607,10 @@ class Templates(unittest.TestCase): # Call methods returned from dictionary lookups 'basic-syntax38': ('{{ var.callable }}', {"var": {"callable": lambda: "foo bar"}}, "foo bar"), + 'builtins01': ('{{ True }}', {}, "True"), + 'builtins02': ('{{ False }}', {}, "False"), + 'builtins03': ('{{ None }}', {}, "None"), + # List-index syntax allows a template to access a certain item of a subscriptable object. 'list-index01': ("{{ var.1 }}", {"var": ["first item", "second item"]}, "second item"),