有趣的Django`NoReverseMatch错误`

时间:2015-03-29 20:26:07

标签: python html django

一切正常,直到我决定在我的模板的某些部分使用django extends来使它们可重复使用,现在我得到了 NoReverseMatch error

url.py

url(r'^school/(?P<id>\d+)/(?P<name>[-\w]+)/$', views.school_profile, name='school'),

views.py

def school_profile(request, id, name):
    #Some actions here...

的index.html

<p class='mini_links'><a href="{% url 'mysite:school' user.school.id user.school|slugify %}">{{user.school|title}} </a></p>

这就是产生错误的html行,我不能为我的生活弄明白。

注意:

正式的html链接只在index.html中,但我现在已经提取(extract.html)了,它现在正在通过:

的index.html

{% extends 'mysite/base.html' %}

{% block mainBody %} 
    {% include 'mysite/extract.html' %}
{% endblock %}

我真的无法弄清楚我做得不对。

错误堆栈:     NoReverseMatch at / mysite /

Reverse for 'school' with arguments '('', u'')' and keyword arguments '{}' not found. 0 pattern(s) tried: []

Request Method:     GET
Request URL:    http://127.0.0.1:8000/mysite/
Django Version:     1.6
Exception Type:     NoReverseMatch
Exception Value:    

Reverse for 'school_profile' with arguments '('', u'')' and keyword arguments '{}' not found. 0 pattern(s) tried: []

Exception Location:     C:\Python27\lib\site-packages\django\core\urlresolvers.py in _reverse_with_prefix, line 429
Python Executable:  C:\Python27\python.exe
Python Version:     2.7.9
Python Path:    

['C:\\Python27\\Lib\\site-packages\\HubEnv\\Scripts\\myApp',
 'C:\\Python27\\lib\\site-packages\\mysql_python-1.2.5-py2.7-win32.egg',
 'C:\\windows\\system32\\python27.zip',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\plat-win',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Python27\\lib\\site-packages']

Server time:    Sun, 29 Mar 2015 21:50:52 +0100

1 个答案:

答案 0 :(得分:1)

您正在尝试撤消命名空间的网址,但您似乎没有定义一个。只需从您的反向删除mysite:命名空间:

{% url 'school' user.school.id user.school|slugify %}

更新:您的代码和堆栈跟踪未相互确认。看起来您没有粘贴正在运行的实际代码。因此,要么将您的反向更改为{% url 'mysite:school' %},要么将您的网址名称更改为..., name='school_profile')