简体   繁体   中英

Django use URL tag in template from app specific urls?

I have the following URLs in my project urls.py

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

So I maintain urls in the register app, so the urls.py in my register 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: []

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

urls.py

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

urls.py in register app:

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

Django doc about url namespace .

project urls.py

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

app urls.py

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

template.html

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM