Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #17229 -- Allow 'True', 'False' and 'None' to resolve to the co…

…rresponding Python objects in templates.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17894 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 93240b7d903af86e135c3b1724a81d2daf31441f 1 parent 28e5b66
@aaugustin aaugustin authored
View
5 django/template/context.py
@@ -18,7 +18,10 @@ def __init__(self, dict_=None):
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))
View
14 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
----------------------------
View
8 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
=====================================
View
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
View
9 tests/regressiontests/templates/tests.py
@@ -372,13 +372,11 @@ def test_url_reverse_no_settings_module(self):
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 @@ def test_no_wrapped_exception(self):
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 @@ def get_template_tests(self):
# 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"),
Please sign in to comment.
Something went wrong with that request. Please try again.