Browse Source

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
Aymeric Augustin 13 years ago
parent
commit
93240b7d90

+ 4 - 1
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))

+ 13 - 1
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
 ----------------------------
 

+ 8 - 0
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
 =====================================
 

+ 0 - 6
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

+ 5 - 4
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"),