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 %-)