gpt4 book ai didi

python - 有趣的 Django `NoReverseMatch error`

转载 作者:太空宇宙 更新时间:2023-11-03 17:49:12 25 4
gpt4 key购买 nike

一切都正常,直到我决定对模板的某些部分使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com