django NoReverseMatch 错误问题
对照这django官方教程(1.8)写第一个APP,在第3部分(Removing hardcoded URLs in templates),将index.html的链接<a href="/polls/{{ question.id }}/">
更改为<a href="{% url 'polls:detail' question.id %}">
,期望输出polls/1
之类的网址。
运行测试网站http://127.0.0.1:8000/polls/
时却发生错误:
NoReverseMatch at /polls/
Reverse for 'detail' with arguments '(2,)' and keyword arguments '{}' not found. 1 pattern(s) tried:[u'$(?P<pk>[0-9]+)/$']
百思不得其解,为什么url解析错误。
当时的polls/urls.py:
from django.conf.urls import urlfrom . import viewsurlpatterns = [url(r'^$', views.IndexView.as_view(), name='index'),url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
polls/template/polls/index.html:
{% if latest_question_list %}<ul>{% for question in latest_question_list %}<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>{% endfor %}</ul>
{% else %}<p>No polls are available.</p>
{% endif %}
查看url tag的文档,里面是这么描述url tag的功能。
url
Returns an absolute path reference (a URL without the domain name) matching a given view and optional parameters.For example, suppose you have a view, app_views.client, whose URLconf takes a client ID (here, client() is a method inside the views file app_views.py). The URLconf line might look like this:
('^client/([0-9]+)/$', app_views.client, name='app-views-client')
If this app’s URLconf is included into the project’s URLconf under a path such as this:
('^clients/', include('project_name.app_name.urls'))
...then, in a template, you can create a link to this view like this:
{% url 'app-views-client' client.id %}
The template tag will output the string /clients/client/123/.
Note that if the URL you’re reversing doesn’t exist, you’ll get an NoReverseMatch exception raised, which will cause your site to display an error page.
说的是不存在想要反析的URL就产生NoReverseMatch异常,反析的过程应该就是让网址括号里的正则表达式里匹配传过去的参数(参考Reverse resolution of URLs)。
在之前的index.html,question.id作为参数传过去,结果不匹配。竟怀疑question.id不为数字,直接将参数改为10,结果仍然是产生了NoReverseMatch异常:
NoReverseMatch at /polls/
Reverse for 'detail' with arguments '(10,)' and keyword arguments '{}' not found. 1 pattern(s) tried:[u'$(?P<pk>[0-9]+)/$']
但注意到参数发生了变化,确实变成了10, arguments '(**10**,)'
。既然传输的参数是正确的,那问题还是出在正则表达式的网址上。
仔细查看错误提示,试验了1个正则表达式(1 pattern(s) tried: [u'$(?P<pk>[0-9]+)/$']
),这个试验的正则表达式有点怪异,前面居然有一个匹配结尾元字符$
,而polls/urls.py中name='detail'对应的网址正则表达式为r'^(?P<pk>[0-9]+)/$'
。
逐想到上层mysite/urls.py中的配置,查看mysite/urls.py:
from django.conf.urls import include, url
from django.contrib import adminurlpatterns = [url(r'^$', include('polls.urls', namespace="polls")),url(r'^polls/', include('polls.urls', namespace="polls")),url(r'^admin/', include(admin.site.urls)),
]
居然定义了两个namespace="polls",当执行{% url 'polls:detail' question.id %}
时,django先找到mysite/polls第一个namespace=polls的url regex,然后加上polls/urls.py对应name=detail的url regex,得到最后的url regex: $(?P<pk>[0-9]+)/$
,这个正则式要求第一个位置匹配结束,这估计怎么都不会反析成功。而正确的url regex应该是第二个namespace=polls的url regex加上polls/urls.py对应name=detail的url regex,既polls/(?P<pk>[0-9]+)/$
。
发现问题所在后,将第一个namespace=polls所在行删掉,再次打开http://127.0.0.1:8000/polls/
,正确的网页显示出来了,而且每一个问题对应的网址就是http://127.0.0.1:8000/polls/[数字]
。
django先应用找到的第一个namespace,所以将第一个namespace和第二个互换位置,结果也是可以正常显示。
惨痛的教训告诫我们,不要在网站的urls.py使用同一个名字的namespace。另外,网站的urls.py若使用了include,不要在正则式后再加$
。参考官方文档:Including other URLconfs
转载于:https://www.cnblogs.com/kekeyu/p/4948555.html
django NoReverseMatch 错误问题相关推荐
- django forms 错误处理
源码文件,site-packages/django/forms/forms.py 一个form实例. Form.errors 返回一个ErrorDict实例,包含当前表单所有的错误,可以在is_val ...
- django 创建mysql失败_创建表时出现Django MySQL错误
我正在用MySQL数据库构建一个django应用程序.当我第一次运行"python manage.py migrate"时,一些表创建得很好,然后出现一些错误.出现的错误是:dja ...
- django 403 错误:CSRF token missing or incorrect
最近觉得SAE不错,就开始试试看,从Django开始.把原来MVC3的一个小项目转过来,记录下碰到的问题. 用的Django版本为1.4. 我比较喜欢用jquery的ajax来做表单提交(我觉得aja ...
- django数据库错误django. db. utils. OperationalError: ( 1044,‘Access denied for user’ erqueque’@' %’to‘dj’
数据库配置的两种方法 第一种,在settings内直接配置,缺点就是如果上传到GitHub或码云会把数据库给暴露出来 DATABASES = { 'default': { 'ENGINE': 'dja ...
- django解决错误Reverse for 'show' with arguments '()' and keyword arguments '{}' not found. 办法
保留个犯罪现场吧: 出现的错误 NoReverseMatch at / Reverse for 'show' with arguments '()' and keyword arguments '{} ...
- django产生错误,Unknown field(s) () specified for StoreEntity. Check fields/fieldsets/exclude attributes
更新数据表,添加ImageField之后再次刷新admin页面之后,产生如此错误. 搜索之后未查找到. 后来想到,在迁移的时候我的项目同时在运行,可能是数据未来得及更新,于是重新python mana ...
- django莫名其妙错误-It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format.]
migrate报错[ value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format.] 在m ...
- 使用 Django 的日志模块,同时发送错误邮件到163邮箱
官方文档: https://docs.djangoproject.com/zh-hans/2.2/topics/logging/ Django 版本 2.2 Django 使用 Python 内置的 ...
- Django发生Article.author: (fields.E304) Reverse accessor for 'Article.author'错误
Django发生错误如下: SystemCheckError: System check identified some issues: ERRORS: content.Article.author: ...
最新文章
- JavaSE学习笔记(二十九)—— 其它流
- 定位相关论文-A Novel Pedestrian Dead Reckoning Algorithm for Multi-Mode Recognition Based on Smartphones
- MessageBoxA 和MessageBoxW
- 求素数——多线程练习
- 简谈C/C++学习路线
- pdffactory字体打印不对_标准论文格式字体要求
- C++map容器-大小和互换
- oracle 存储过程打印语句,oracle学习之第一个存储过程:打印Hello World
- 你能打动客户的C++理由,一定要先说服自己相信
- fiddler重定向
- IIS 搭建简单网站 并 发布项目
- vfp 使用 .Net 系统自带的组件
- java代码实现端口是否ping通
- matlab设计计算器,MATLAB-计算器的设计.doc
- 计算机考研公共课考英语几,新文道教育:2022考研必须要了解的30个知识点
- 为什么架构师工资比运维高?
- MySQL期末试题的答案
- chrome浏览器调试JS代码
- android自定义View之margin和padding的处理
- 秋雨,在迷乱的思绪中飞扬