Skip to content

Commit f48cf95

Browse files
committed
[1.1.X] Fixed #12070. Fixed a case where var._whatever wasn't raising a TemplateSyntaxError. Backport of r12539 from trunk.
git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12540 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent ae01b0c commit f48cf95

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

django/template/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,6 @@ def __init__(self, token, parser):
518518
var_obj = None
519519
elif var is None:
520520
raise TemplateSyntaxError("Could not find variable at start of %s." % token)
521-
elif var.find(VARIABLE_ATTRIBUTE_SEPARATOR + '_') > -1 or var[0] == '_':
522-
raise TemplateSyntaxError("Variables and attributes may not begin with underscores: '%s'" % var)
523521
else:
524522
var_obj = Variable(var)
525523
else:
@@ -678,6 +676,8 @@ def __init__(self, var):
678676
except ValueError:
679677
# Otherwise we'll set self.lookups so that resolve() knows we're
680678
# dealing with a bonafide variable
679+
if var.find(VARIABLE_ATTRIBUTE_SEPARATOR + '_') > -1 or var[0] == '_':
680+
raise TemplateSyntaxError("Variables and attributes may not begin with underscores: '%s'" % var)
681681
self.lookups = tuple(var.split(VARIABLE_ATTRIBUTE_SEPARATOR))
682682

683683
def resolve(self, context):

tests/regressiontests/templates/parser.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
[]
2828
>>> fe.var
2929
u'Some "Good" News'
30+
31+
Filtered variables should reject access of attributes beginning with underscores.
32+
33+
>>> FilterExpression('article._hidden|upper', p)
34+
Traceback (most recent call last):
35+
...
36+
TemplateSyntaxError: Variables and attributes may not begin with underscores: 'article._hidden'
3037
"""
3138

3239
variable_parsing = r"""
@@ -56,4 +63,10 @@
5663
>>> Variable(ur"'Some \'Better\' News'").resolve(c)
5764
u"Some 'Better' News"
5865
66+
Variables should reject access of attributes beginning with underscores.
67+
68+
>>> Variable('article._hidden')
69+
Traceback (most recent call last):
70+
...
71+
TemplateSyntaxError: Variables and attributes may not begin with underscores: 'article._hidden'
5972
"""

0 commit comments

Comments
 (0)