Skip to content

Commit

Permalink
Fixed django#17229 -- Allow 'True', 'False' and 'None' to resolve to …
Browse files Browse the repository at this point in the history
…the corresponding Python objects in templates.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17894 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
aaugustin committed Apr 10, 2012
1 parent 28e5b66 commit 93240b7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
5 changes: 4 additions & 1 deletion django/template/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
14 changes: 13 additions & 1 deletion docs/ref/templates/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
----------------------------

Expand Down
8 changes: 8 additions & 0 deletions docs/releases/1.5.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
=====================================

Expand Down
6 changes: 0 additions & 6 deletions docs/topics/i18n/timezones.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions tests/regressiontests/templates/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}")
Expand All @@ -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
Expand Down Expand Up @@ -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"),

Expand Down

0 comments on commit 93240b7

Please sign in to comment.