2

I am using named url-patterns in my Django app, but it is failing and I don't know why. I have followed the docs but my app errors out when it reads urls.py.

This works:

urls.py

from django.conf.urls import patterns
from www.apps.newsletter.models import Newsletter
from www.apps.newsletter.views import index, detail

urlpatterns = patterns('',
    (r'^vol(?P<q_vol>\d+)/no(?P<q_no>\d+)/(?P<q_slug>[^.*]+)/$', detail),
    (r'^vol(?P<q_vol>\d+)/no(?P<q_no>\d+)/$', index),
    (r'^vol(?P<q_vol>\d+)/$', index),
    (r'^$', index),
)

But this fails:

urls.py

from django.conf.urls import patterns
from www.apps.newsletter.models import Newsletter
from www.apps.newsletter.views import index, detail

urlpatterns = patterns('',
    (r'^vol(?P<q_vol>\d+)/no(?P<q_no>\d+)/(?P<q_slug>[^.*]+)/$', detail, name='newsletter-detail'),
    (r'^vol(?P<q_vol>\d+)/no(?P<q_no>\d+)/$', index),
    (r'^vol(?P<q_vol>\d+)/$', index),
    (r'^$', index),
)

I want to use named url patterns (so I can use {% url %} in my template), but Django doesn't seem to want to play nice. Any ideas? All I am doing is adding the arg name="" and it causes the error:

SyntaxError at /newsletter/

invalid syntax (urls.py, line 6)

This urls.py is referenced by the parent urls.py:

urlpatterns = patterns('',
    url(r'^grappelli/', include('grappelli.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^software/', include('www.apps.software.urls')),
    url(r'^newsletter/', include('www.apps.newsletter.urls')),
)

I am using Django version 1.4. TIA.

3
  • 1
    possible duplicate of why this url pattern is not working? Commented Aug 21, 2012 at 21:48
  • @IgnacioVazquez-Abrams: Thanks - you are correct. Should I delete this question then? Commented Aug 21, 2012 at 22:01
  • It should get closed eventually. Commented Aug 21, 2012 at 22:02

1 Answer 1

3

You need to use the url function to make use of the name parameter:

url(r'^vol(?P<q_vol>\d+)/no(?P<q_no>\d+)/(?P<q_slug>[^.*]+)/$', detail, name='newsletter-detail'),

In other words, there is a difference between using a tuple and the url function.

Btw, you don't need to have named url patterns to make use of the url template tag, you can just use the view's path instead, i.e. {% url "app.views.myview" %}

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Thought it must be something simple like that. Must remember to RTFM more thoroughly....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.