python - 有趣的 Django `NoReverseMatch error`

标签 python html django

一切都正常,直到我决定对模板的某些部分使用 django extends 以使它们可重用,现在我得到了 NoReverseMatch 错误

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 位于/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

最佳答案

您正在尝试反转命名空间 URL,但您似乎尚未定义该 URL。只需从反向中删除 mysite: 命名空间即可:

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

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

关于python - 有趣的 Django `NoReverseMatch error`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29334442/

相关文章:

Django: django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint' )

python - 如何将 CSS 添加到我的 Django 元素网站?

python - Django 中的 URLField() 可以加载本地主机路径吗?

python - 伯努利朴素贝叶斯错误 : ValueError: Unknown label type: (array([0, 0, 0, ..., 0, 0, 0], dtype=object),)

python - 如何解决错误 :SDL_OpenAudio (2 channels, 44​​100 Hz):没有这样的音频设备

javascript - 单击页面任意位置时隐藏侧边栏

jquery - 单击 DIV 内的可单击图像不应影响 DIV 本身

python - 如何更改 pandas 数据框中的文本片段

python - 快速 Python 序列化到 Flatbuffer?

php - 对于为什么 </option> 标签在第二个回显结束时没有打印有什么建议吗?