TemplateSyntaxError with {% cycle "Hello, how are you?" "Fine!" %}

56 views
Skip to first unread message

Carsten Fuchs

unread,
Mar 5, 2015, 11:26:19 AM3/5/15
to django...@googlegroups.com
Dear Django fellows,

using Django 1.7.5, I have a problem with commas in string literals in
the cycle tag, e.g. with

{% cycle "Hello, how are you?" "Fine!" %}.

Please consider (newlines added for clarity):

$ ./manage.py shell

>>> from django.template import *
>>> t = Template('''{% cycle "Hello, how are you?" "Fine!" %}''')
>>>

/home/carsten/.virtualenvs/Ze/local/lib/python2.7/site-packages/django/template/base.py:290:
RemovedInDjango18Warning: 'The `cycle` template tag is changing to
escape its arguments; the non-autoescaping version is deprecated. Load
it from the `future` tag library to start using the new behavior.
compiled_result = compile_func(self, token)

Traceback (most recent call last):
File "<console>", line 1, in <module>
File
"/home/carsten/.virtualenvs/Ze/local/lib/python2.7/site-packages/django/template/base.py",
line 132, in __init__
self.nodelist = compile_string(template_string, origin)
File
"/home/carsten/.virtualenvs/Ze/local/lib/python2.7/site-packages/django/template/base.py",
line 162, in compile_string
return parser.parse()
File
"/home/carsten/.virtualenvs/Ze/local/lib/python2.7/site-packages/django/template/base.py",
line 290, in parse
compiled_result = compile_func(self, token)
File
"/home/carsten/.virtualenvs/Ze/local/lib/python2.7/site-packages/django/template/defaulttags.py",
line 648, in cycle
values = [parser.compile_filter(arg) for arg in args[1:]]
File
"/home/carsten/.virtualenvs/Ze/local/lib/python2.7/site-packages/django/template/base.py",
line 372, in compile_filter
return FilterExpression(token, self)
File
"/home/carsten/.virtualenvs/Ze/local/lib/python2.7/site-packages/django/template/base.py",
line 588, in __init__
"from '%s'" % (token[upto:], token))
TemplateSyntaxError: Could not parse the remainder: 'Hello"' from '""Hello"'


This happens with or without {% load cycle from future %}

A bug? Or am I doing something wrong?

Best regards,
Carsten

Vijay Khemlani

unread,
Mar 5, 2015, 11:48:52 AM3/5/15
to django...@googlegroups.com
Changing the double quotes for single quotes seems to do the trick, but I don't know why it works



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/54F8834D.7010403%40cafu.de.
For more options, visit https://groups.google.com/d/optout.

Alasdair Nicol

unread,
Mar 5, 2015, 11:53:59 AM3/5/15
to django...@googlegroups.com
Hi Carsten, Vijay

On 05/03/15 16:24, Carsten Fuchs wrote:
> Dear Django fellows,
>
> using Django 1.7.5, I have a problem with commas in string literals in
> the cycle tag, e.g. with
>
> {% cycle "Hello, how are you?" "Fine!" %}.

I can reproduce the problem with your sample code.

I think the reason is that the cycle tag supports an older syntax for
backwards compatibility reasons.

{% cycle row1,row2,row3 %}

It looks like this prevents you from using commas in strings when using
the new syntax.

> This happens with or without {% load cycle from future %}

The future version of the cycle tag applies HTML escaping to its
variables. So using it or not using it shouldn't have any affect on your
issue.

A work around would be to assign "Hello, how are you" to a variable.

{% with hello="Hello, how are you?"
{% cycle hello "Fine!" %}.
{% endwith %}

On 05/03/15 16:48, Vijay Khemlani wrote:
> Changing the double quotes for single quotes seems to do the trick,
but I don't know why it works

If you use single quotes and render the template, it outputs 'Hello',
not 'Hello, how are you?' as expected.

Cheers,
Alasdair

[1] https://docs.djangoproject.com/en/1.7/ref/templates/builtins/#cycle

--
Alasdair Nicol
Developer, MEMSET

mail: alas...@memset.com
web: http://www.memset.com/

Memset Ltd., registration number 4504980.
Building 87, Dunsfold Park, Stovolds Hill, Cranleigh, Surrey, GU6 8TB, UK

Alasdair Nicol

unread,
Mar 5, 2015, 11:55:24 AM3/5/15
to django...@googlegroups.com
On 05/03/15 16:53, Alasdair Nicol wrote:
> {% with hello="Hello, how are you?"
> {% cycle hello "Fine!" %}.
> {% endwith %}

Oops, that should have read:

{% with hello="Hello, how are you?" %}
{% cycle hello "Fine!" %}.
{% endwith %}

Carsten Fuchs

unread,
Mar 5, 2015, 12:24:32 PM3/5/15
to django...@googlegroups.com
Hi Vijay, hi Alasdair,

many thanks for your quick replies!
I've thus just filed a bug: https://code.djangoproject.com/ticket/24451

Am 05.03.2015 um 17:53 schrieb Alasdair Nicol:
> I think the reason is that the cycle tag supports an older syntax for
> backwards compatibility reasons.

Yes, that was my thought, too, but I was wondering if I was accidentally
triggering the old syntax in a somehow "correct" manner that I didn't
quite see...

> A work around would be to assign "Hello, how are you" to a variable.

Yes, thanks!

> If you use single quotes and render the template, it outputs 'Hello',
> not 'Hello, how are you?' as expected.

As expected? Why please? Docs say that with single or double quotes, the
values are treated as string literals.

Best regards,
Carsten

Alasdair Nicol

unread,
Mar 6, 2015, 5:08:50 AM3/6/15
to django...@googlegroups.com
On 05/03/15 17:23, Carsten Fuchs wrote:
>> If you use single quotes and render the template, it outputs 'Hello',
>> not 'Hello, how are you?' as expected.
>
> As expected? Why please? Docs say that with single or double quotes, the
> values are treated as string literals.

Sorry, I wasn't clear. By 'as expected', I meant that you want the cycle
tag to return "Hello, how are you". Switching to single quotes isn't a
fix, because it only returns 'Hello'.

Cheers,
Alasdair
Reply all
Reply to author
Forward
0 new messages