{%url%}给了我NoReverseMatch错误,而reverse()返回的url很好。为什么? [英] {% url %} gives me NoReverseMatch error while reverse() returns the url just fine. Why?
问题描述
我不知道这个这个问题与我即将描述的同样的问题,但它确实有同样的症状。不幸的是,在写我的时候,它仍然没有得到解决。
I don't know if this SO question is of the same problem that I am about to describe, but it does share the same symptoms. Unfortunately, it still remains unresolved as I am writing.
所以这里是我的问题。我正在尝试将James Bennett的 django-registration app 添加到我的django项目中。我几乎完成了配置我的需要 - 自定义模板和网址。就在我以为一切都很好的时候我使用 {%url'testing'item_id = 123%}
中的 NoReverseMatch
(我也尝试使用视图在django注册所需的自定义模板之一中,名称 myapp.views.test
而不是运气)。有趣的是,我在shell中尝试了 reverse('testing',kwargs = {'item_id':123})
,并且返回的url很好。我认为在后端, {%url%}
使用 reverse()
,但为什么会得到不同的结果?
So here is my problem. I am trying to add James Bennett's django-registration app to my django project. I have pretty much finished configuring it to my needs - custom templates and urls. Just when I thought everything was good to go. I got NoReverseMatch
error from using {% url 'testing' item_id=123 %}
(I also tried using the view name, myapp.views.test
, instead but no luck) in one of the custom templates required by django-registration. Interestingly, I tried reverse('testing', kwargs={'item_id':123})
in the shell and the url was returned just fine. I thought {% url %}
uses reverse()
in the back-end but why did I get different outcomes?
urls.py:(我网站的URLconf)
urls.py: (the URLconf of my site)
urlpatterns = patterns('myapp.views',
url(r'^test/(?P<item_id>\d+)/$', 'test', name='testing'),
)
activate_email.txt:说明模板,请注意,django注册要求的是 .txt 扩展名,这不应该是问题的原因。)
activation_email.txt: (the said template. Note it's intentionally in .txt extension as required by django-registration and that shouldn't be the cause of the problem.)
{% comment %}Used to generate the body of the activation email.{% endcomment %}
Welcome to {{ site }}! Please activate your account by clicking on the following link:
{% url 'testing' item_id=123 %}
Note the activation link/code will be expired in {{ expiration_days }} days.
我不知道是否重要,但只是想我应该提到 activation_email.txt 存储在 myapp
的模板目录中,尽管它由 django-registration
。
I don't know if it matters but just thought I should mention activation_email.txt is stored in the templates directory of myapp
though it is used by django-registration
.
此外,我使用django 1.4
Also, I am using django 1.4
我有一种感觉,这个问题与url命名空间,一个我从来没有理解的主题,但这只是一个天真的猜测。 (IMO,django文档很好地解释了关于django的所有内容,除了涉及url的命名空间)
I have a feeling that the problem has something to do with the url namespaces, a topic that I have never understood, but it's just a naive guess. (IMO, the django documentation is great in explaining everything about django, except when it comes to url namespaces)
推荐答案
在这里没有专家,但是在目前我正在使用的Django项目中,我使用没有引号的url的名称。我刚刚在我的一个模板中添加了一个类似行的引号,并产生与错误相同的错误。
I'm no expert here, but in a Django project I'm working on at the moment I use the name of the url without quotes. I just added quotes around a similar line in one of my templates and it produced the same error as your error.
尝试:
{% url testing item_id=123 %}
这篇关于{%url%}给了我NoReverseMatch错误,而reverse()返回的url很好。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!