[英]Django use URL tag in template from app specific urls?
I have the following URLs in my project urls.py
我的项目
urls.py
有以下URL
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: 所以,我保持寄存器的应用程序网址,所以
urls.py
在我register
的应用程序是这样的:
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: 所以我有一个模板,它位于register应用程序的外部,位于项目的根目录中,并且我试图像这样使用上面的URL:
<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>
? 您是否尝试过:
<a href="{% url 'register:register_test' %}"/>Test</a>
? You might want to give the register urls a name space first though: 您可能想先给注册网址一个名称空间:
urls.py urls.py
urlpatterns = i18n_patterns(
url(r'^register/', include('register.urls', namespace='register'),
)
urls.py in register app: 注册应用中的urls.py:
urlpatterns = patterns(
'register.views',
url(r'^test/$', 'Test', name="register_test"),
)
Django doc about url namespace . 有关url名称空间的 Django文档。
project urls.py 项目urls.py
urlpatterns = i18n_patterns(
url(r'^register/', include('register.urls', namespace="register")),
)
app urls.py 应用urls.py
urlpatterns = patterns(
url(r'^test/$', views.Test, name="register_test"),
)
template.html template.html
<a href="{% url 'register:register_test' %}"/>Test</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.