#19882: Forloop with a filter that has an argument containing whitespace raises
a
TemplateSyntaxError
--------------------------+------------------------------------------------
Reporter: | Owner: nobody
bmispelon | Status: new
Type: Bug | Version: master
Component: Template | Keywords: forloop filter TemplateSyntaxError
system | Has patch: 0
Severity: Normal | UI/UX: 0
Triage Stage: |
Unreviewed |
Easy pickings: 0 |
--------------------------+------------------------------------------------
In a template, when looping over a variable that has a filter with an
argument containing whitespace, a `TemplateSyntaxError` will be thrown.
Here's a small test case to reproduce the issue (the first template fails
while the other two work as expected):
{{{#!python
from django.template import Template, Context
for tpl in [
'{% for x in ""|add:"a b c" %}{{ x }}{% endfor %}', # Doesn't work
'{% with v=""|add:"a b c" %}{% for x in v %}{{ x }}{% endfor %}{%
endwith %}',
'{% with v="a b c" %}{% for x in ""|add:v %}{{ x }}{% endfor %}{%
endwith %}'
]:
s = Template(tpl).render(Context())
assert s == u'a b c'
}}}
Here's the traceback that this produces:
{{{
Traceback (most recent call last):
File "my_script.py", line 8, in <module>
s = Template(tpl).render(Context())
File "/path/to/venv/local/lib/python2.7/site-
packages/django/template/base.py", line 125, in __init__
self.nodelist = compile_string(template_string, origin)
File "/path/to/venv/local/lib/python2.7/site-
packages/django/template/base.py", line 153, in compile_string
return parser.parse()
File "/path/to/venv/local/lib/python2.7/site-
packages/django/template/base.py", line 278, in parse
compiled_result = compile_func(self, token)
File "/path/to/venv/local/lib/python2.7/site-
packages/django/template/defaulttags.py", line 758, in do_for
" 'for x in y': %s" % token.contents)
django.template.base.TemplateSyntaxError: 'for' statements should use the
format 'for x in y': for x in ""|add:"a b c"
}}}
It looks like `token.contents.split()` in the code of the tag gets
confused by the whitespace and splits the string into too many parts.
Since the code searches for the "in" keyword from the end (position -2
since we are not reversed), then it fails.
--
Ticket URL: <https://code.djangoproject.com/ticket/19882>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.