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
This commit is contained in:
Aymeric Augustin 2012-04-10 20:49:45 +00:00
parent 28e5b66518
commit 93240b7d90
5 changed files with 30 additions and 12 deletions

View File

@ -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))

View File

@ -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 File

@ -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 File

@ -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 File

@ -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"),