简体   繁体   English

{%url%}给出NoReverseMatch错误

[英]{% url %} gives NoReverseMatch error

I'm using Django 1.4 and am getting the following error 我正在使用Django 1.4并收到以下错误

Reverse for 'dj' with arguments '()' and keyword arguments '{'dj_name': u"John O'Callaghan"}' not found.

Using John OCallaghan instead of John O'Callaghan in the database works perfectly fine and doesn't give an error. 在数据库中使用John OCallaghan代替John O'Callaghan可以很好地工作,并且不会产生错误。

Here is the line that gives the error 这是给出错误的行

<a href="{% url 'dj' dj_name=dj.name %}"><img src="{{ MEDIA_URL }}{{ dj.img }}" class="img-rounded" id="dj_img"/></a>

I read at quite a few places that you are not supposed to use quotes in {% url %} in Django 1.4. 我在很多地方读到,在Django 1.4中,您不应该在{% url %}中使用引号。 Using {% url dj dj_name=dj.name %} instead of {% url 'dj' dj_name=dj.name %} gives the following error instead 使用{% url dj dj_name=dj.name %}代替{% url 'dj' dj_name=dj.name %}会产生以下错误

TypeError - cannot concatenate 'str' and 'DJ' objects

urls.py urls.py

urlpatterns = patterns('hunt.views',
    url(r'^$', views.landing, name='landing'),
    url(r'^top100/$', views.top100, name='top100'),
    url(r'^top100/(?P<dj_name>[a-zA-Z0-9 &-]+)/$', views.dj, name='dj'),
)

Having seen your URL, I'm surprised at your surprise that it is not working with "O'Callaghan". 看到您的网址后,我惊讶于它不能与“ O'Callaghan”一起使用。 Your regex specifies exactly the characters that it accepts: a to z, A to Z, 0 to 9, space, ampersand, and hyphen. 您的正则表达式精确指定了它可以接受的字符:a到z,A到Z,0到9,空格,“&”和连字符。 Apostrophe is not there. 撇号不在那里。 If you want it to accept an apostrophe, you need to add it to that list. 如果希望它接受撇号,则需要将其添加到该列表中。

r"^top100/(?P<dj_name>[a-zA-Z0-9 &-']+)/$"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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