I think I need a second pair of eyes.
我想我需要第二双眼睛。
The below example should be self explanatory.
以下示例应该是自解释的。
All I need is to be able to reverse my url in the template.
我只需要能够在模板中反转我的网址。
/urls.py
urlpatterns = patterns('',
(r'^products/', include('products.urls')),
)
/products/urls.py
from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('products.views',
url(r'^$', view="index", name="index"),
)
/templates/products/index.html
<a href="{% url products:index %}"> Products </a>
UPDATE
Full stacktrace - http://pastebin.com/9nLp4uP5
完整的堆栈跟踪 - http://pastebin.com/9nLp4uP5
2 个解决方案
#1
#2
6
You might try this instead:
您可以尝试这样做:
urlpatterns = patterns('products.views',
url(r'^$', view="index", name="products_index"),
)
/templates/products/index.html
<a href="{% url products_index %}"> Products </a>
Unless there's a compelling reason you want to namespace your urls, it's way easier just to use a more precise name in urls.py
and then use that name in the url
template tag.
除非有令人信服的理由想要命名您的网址,否则只需在urls.py中使用更精确的名称,然后在网址模板标记中使用该名称即可。
Update
If the error you're getting is No module named urls
then that means one of the urls.py files isn't being read in by the django project. Did you make sure that products
has been added to INSTALLED_APPS
in the settings.py
file? Also, please include a stacktrace in your question so it will be easier to identify where the error is taking place.
如果你得到的错误是没有名为urls的模块,那么这意味着django项目没有读取其中一个urls.py文件。您确定已将产品添加到settings.py文件中的INSTALLED_APPS吗?此外,请在您的问题中包含堆栈跟踪,以便更容易识别错误发生的位置。
#1
14
The syntax changed after Django 1.5 Instead of doing this:
Django 1.5后语法改变而不是这样做:
<a href="{% url products_index %}"> Products </a>
You should now do this(string instead):
你现在应该这样做(字符串代替):
<a href="{% url 'products_index' %}"> Products </a>
#2
6
You might try this instead:
您可以尝试这样做:
urlpatterns = patterns('products.views',
url(r'^$', view="index", name="products_index"),
)
/templates/products/index.html
<a href="{% url products_index %}"> Products </a>
Unless there's a compelling reason you want to namespace your urls, it's way easier just to use a more precise name in urls.py
and then use that name in the url
template tag.
除非有令人信服的理由想要命名您的网址,否则只需在urls.py中使用更精确的名称,然后在网址模板标记中使用该名称即可。
Update
If the error you're getting is No module named urls
then that means one of the urls.py files isn't being read in by the django project. Did you make sure that products
has been added to INSTALLED_APPS
in the settings.py
file? Also, please include a stacktrace in your question so it will be easier to identify where the error is taking place.
如果你得到的错误是没有名为urls的模块,那么这意味着django项目没有读取其中一个urls.py文件。您确定已将产品添加到settings.py文件中的INSTALLED_APPS吗?此外,请在您的问题中包含堆栈跟踪,以便更容易识别错误发生的位置。