lastmjs
lastmjs

Reputation: 1014

Django url in template - NoReverseMatch at /

I cannot figure this out. Here is the url pattern I have in my app urls.py

url(r'^(P<categoryName>[a-z]+)/$', views.displayCategory, name='displayCategory'),

Here is my project's global urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', include('publicworkspace.urls', namespace="publicworkspace")),
    url(r'^createproblem/', include('createproblem.urls', namespace="createproblem")),
    url(r'^publicproblem/', include('publicproblem.urls', namespace="publicproblem")),
    url(r'^admin/', include(admin.site.urls)),
)

And here is the link I want to create in my template < a href="{% url 'publicworkspace:displayCategory' 'math' %}">Math

Everytime I get an error, usually the following:

NoReverseMatch at /
Reverse for 'displayCategory' with arguments '(u'math',)' and keyword arguments '{}' not                    found. 1 pattern(s) tried: [u'$(P<categoryName>[a-z]+)/$']

Upvotes: 0

Views: 247

Answers (3)

steffens21
steffens21

Reputation: 759

The regexp r'^$' in your first urlpatterns line is probably not what you want. It will only match an empty string.

I suggest the following:

urlpatterns = patterns('',
    url(r'^createproblem/', include('createproblem.urls', namespace="createproblem")),
    url(r'^publicproblem/', include('publicproblem.urls', namespace="publicproblem")),
    url(r'^admin/', include(admin.site.urls)),
    url(r'', include('publicworkspace.urls', namespace="publicworkspace")),
)

Note that I moved the relevant line to the bottom of urlpatterns. If you keep it at the top it will always match and your other url patterns will never be even looked at (since django takes the first one that matches).

Upvotes: 2

zymud
zymud

Reputation: 2249

url(r'^$', include('publicworkspace.urls', namespace="publicworkspace")),

problem is here. Try:

url(r'^', include('publicworkspace.urls', namespace="publicworkspace")),

Explanation: $ - end-of-string match character, so you need to put when your url is ending.

Upvotes: 1

user764357
user764357

Reputation:

Your url pattern has a named capture, but you aren't telling the url in the template the name you wish to assign a value into.

Try this instead:

< a href="{% url 'publicworkspace:displayCategory' categoryName='math' %}">

Upvotes: 0

Related Questions