diff --git a/django/core/template/__init__.py b/django/core/template/__init__.py index e498fda00c..60bb5e0888 100644 --- a/django/core/template/__init__.py +++ b/django/core/template/__init__.py @@ -629,7 +629,7 @@ def resolve_variable(path, context): """ Returns the resolved variable, which may contain attribute syntax, within the given context. The variable may be a hard-coded string (if it begins - and ends with single or double quote marks). + and ends with single or double quote marks), or an integer or float literal. >>> c = {'article': {'section':'News'}} >>> resolve_variable('article.section', c) @@ -645,7 +645,13 @@ def resolve_variable(path, context): (The example assumes VARIABLE_ATTRIBUTE_SEPARATOR is '.') """ - if path[0] in ('"', "'") and path[0] == path[-1]: + if path[0] in '0123456789': + number_type = '.' in path and float or int + try: + current = number_type(path) + except ValueError: + current = '' + elif path[0] in ('"', "'") and path[0] == path[-1]: current = path[1:-1] else: current = context diff --git a/tests/othertests/templates.py b/tests/othertests/templates.py index a92fc5ef00..7063720c31 100644 --- a/tests/othertests/templates.py +++ b/tests/othertests/templates.py @@ -164,6 +164,20 @@ TEMPLATE_TESTS = { 'ifequal09': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {}, "no"), 'ifequal10': ('{% ifequal a b %}yes{% else %}no{% endifequal %}', {}, "yes"), + # Integers + 'ifequal11': ('{% ifequal a 1 %}yes{% else %}no{% endifequal %}', {}, "no"), + 'ifequal12': ('{% ifequal a 1 %}yes{% else %}no{% endifequal %}', {"a": 1}, "yes"), + 'ifequal13': ('{% ifequal a 1 %}yes{% else %}no{% endifequal %}', {"a": "1"}, "no"), + 'ifequal14': ('{% ifequal a "1" %}yes{% else %}no{% endifequal %}', {"a": 1}, "no"), + 'ifequal15': ('{% ifequal a "1" %}yes{% else %}no{% endifequal %}', {"a": "1"}, "yes"), + + # Floats + 'ifequal16': ('{% ifequal a 1.2 %}yes{% else %}no{% endifequal %}', {}, "no"), + 'ifequal17': ('{% ifequal a 1.2 %}yes{% else %}no{% endifequal %}', {"a": 1.2}, "yes"), + 'ifequal18': ('{% ifequal a 1.2 %}yes{% else %}no{% endifequal %}', {"a": "1.2"}, "no"), + 'ifequal19': ('{% ifequal a "1.2" %}yes{% else %}no{% endifequal %}', {"a": 1.2}, "no"), + 'ifequal20': ('{% ifequal a "1.2" %}yes{% else %}no{% endifequal %}', {"a": "1.2"}, "yes"), + ### IFNOTEQUAL TAG ######################################################## 'ifnotequal01': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 2}, "yes"), 'ifnotequal02': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 1}, ""),