Problem with named urls

73 views
Skip to first unread message

Per-Olof Åstrand

unread,
Jul 14, 2012, 6:09:34 PM7/14/12
to django...@googlegroups.com
I have a small hobby project that I have worked on from time to time over the years. I started to convert my views to class-based views, and realized that I needed to start to use named urls too.

For an url from an url.py like

url(r'^persons/modified/$', 'genealogy.relations.views.persons_modified', name="persons-modified"),

it works with the following entry in the template

<p><a href={% url 'persons-modified' %}>Modified</a></p>

but not with

<p><a href={% url persons-modified %}>Modified</a></p>

which I think is according to the documentation: https://docs.djangoproject.com/en/dev/topics/http/urls/#naming-url-patterns

For the latter case (according to the documentation, I think), I get an error

TemplateSyntaxError at /

Could not parse the remainder: '-modified' from 'persons-modified'
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.5.dev20120714103239
Exception Type: TemplateSyntaxError
Exception Value:
Could not parse the remainder: '-modified' from 'persons-modified'
Exception Location: /Users/aastrand/git/github/django/django/template/base.py in __init__, line 570
Python Executable: /Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python
Python Version: 2.6.5

Can anyone provide an explanation?

Per-Olof


Russell Keith-Magee

unread,
Jul 14, 2012, 9:03:44 PM7/14/12
to django...@googlegroups.com
On Sun, Jul 15, 2012 at 6:09 AM, Per-Olof Åstrand
<p.o.aa...@gmail.com> wrote:
>
> I have a small hobby project that I have worked on from time to time over the years. I started to convert my views to class-based views, and realized that I needed to start to use named urls too.
>
> For an url from an url.py like
>
> url(r'^persons/modified/$', 'genealogy.relations.views.persons_modified', name="persons-modified"),
>
> it works with the following entry in the template
>
> <p><a href={% url 'persons-modified' %}>Modified</a></p>
>
> but not with
>
> <p><a href={% url persons-modified %}>Modified</a></p>
>
> which I think is according to the documentation: https://docs.djangoproject.com/en/dev/topics/http/urls/#naming-url-patterns
>
> For the latter case (according to the documentation, I think), I get an error
>
> TemplateSyntaxError at /
>
> Could not parse the remainder: '-modified' from 'persons-modified'

Hi Per-Olof,

It looks like you have found a place in Django's documentation that
hasn't been updated to reflect a recent change to Django's URL tag.

In Django 1.3, we started phasing in a change to the {% url %} tag. In
Django 1.2, the {% url %} tag assumed that that the first argument
(the URL name) was always a string, and so, it didn't need to be
quoted. So:

{% url persons-modified %}

was interpreted as "find the URL named 'persons-modified'".

In Django 1.3, we introduced a change that made the {% url %} tag a
little more flexible, and more consistent with other tags. This change
modified the interpretation of the {% url %} tag so that the first
argument was interpreted as a template variable. This means that

{% url "persons-modified" %}

is interpreted as "find the URL named 'persons-modified'", and

{% url persons-modified %}

is interpreted as "find the URL with a name that is contained in the
context variable persons-modified". This particular example then
generates a syntax error, because the '-' is interpreted as an
arithmetic expression, which isn't legal in this situation.

In Django 1.3, the "new" {% url %} tag was available, but only if you
put "{% load url from future %}" at the top of your template. In
Django 1.4, any usage of the "old" {% url %} tag would have raised a
DeprecationWarning when you used the template. In Django 1.5, the old
tag has been removed, and replaced with the new-style url tag.
However, we evidently haven't found all the places in our
documentation where the old-style syntax is being used.

The example in the documentation that you pointed out *should* read:

{% url "arch-summary" 1945 %}
{% url "full-archive" 2007 %}

and your template should be using the quoted syntax.

I've opened ticket #18625 [1] to track this problem with Django's
documentation; thanks for the report!

[1] https://code.djangoproject.com/ticket/18625

Yours,
Russ Magee %-)

Per-Olof Åstrand

unread,
Jul 15, 2012, 6:38:32 AM7/15/12
to django...@googlegroups.com


On Sunday, July 15, 2012 3:03:44 AM UTC+2, Russell Keith-Magee wrote:

Hi Per-Olof,

It looks like you have found a place in Django's documentation that
hasn't been updated to reflect a recent change to Django's URL tag.
[....]

I've opened ticket #18625 [1] to track this problem with Django's
documentation; thanks for the report!

[1] https://code.djangoproject.com/ticket/18625

Yours,
Russ Magee %-)

Thanks! I can see that the ticket has already been fixed and committed. Fantastic!

Per-Olof
Reply all
Reply to author
Forward
0 new messages