John John - 1 year ago 48
Python Question

Django use URL tag in template from app specific urls?

I have the following URLs in my project

urlpatterns = i18n_patterns(
url(r'^register/', include('register.urls')),

So I maintain urls in the register app, so the
in my
app looks like this:

urlpatterns = patterns(
url(r'^test/$', views.Test, name="register_test"),

So I have a template that is located outside the register app, is located in the root of the project, and I am trying to use the above url like this:

<a href="{% url 'register_test' %}"/>Test</a>

But I get the following error:

NoReverseMatch at /en/
Reverse for 'register_test' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

Answer Source

Have you tried: <a href="{% url 'register:register_test' %}"/>Test</a>? You might want to give the register urls a name space first though:

urlpatterns = i18n_patterns(
    url(r'^register/', include('register.urls', namespace='register'),
) in register app:

urlpatterns = patterns(
    url(r'^test/$', 'Test', name="register_test"),

Django doc about url namespace.