Pergunta

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]+)/$']
Foi útil?

Solução

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

Outras dicas

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.

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' %}">
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top