Django 中的 NoReverseMatch 异常帮助
我对 python 相当陌生,并遵循 Django 框架教程的第 4 部分 此处。 我正在尝试为民意调查应用程序实现通用视图——我的代码似乎是正确的(据我所知),但是当我尝试投票时,我收到一个 NoReverseMatch 异常,其中指出:
对“polls/poll_results”进行反向操作,未找到参数“(1L,)”和关键字参数“{}”。
在尝试通用视图之前,我的代码工作得很好,但现在我似乎无法查明问题。
这是 poll 目录中我的 urls.py 的代码:
from django.conf.urls.defaults import *
from djtest.polls.models import Poll
info_dict = {
'queryset': Poll.objects.all(),
}
urlpatterns = patterns('',
(r'^$', 'django.views.generic.list_detail.object_list', info_dict),
(r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),
url(r'^(?P<object_id>\d+)/results/$', 'django.views.generic.list_detail.object_detail', dict(info_dict, template_name='polls/results.html'), 'poll_results'),
(r'^(?P<poll_id>\d+)/vote/$', 'djtest.polls.views.vote'),
)
这是views.py:
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from django.core.urlresolvers import reverse
from djtest.polls.models import Poll, Choice
def vote(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
#redisplay form
return render_to_response('polls/poll_detail.html', {
'object': p,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse('poll_results', args=(p.id,)))
我感觉这是一个语法错误,但我找不到它。 预先感谢您的帮助...
I'm fairly new to python and following along with part 4 of the tutorial for the Django framework here. I'm trying to implement generic views for the polls app--my code seems correct (as far as I can tell), but when I try to vote, I get a NoReverseMatch Exception that states:
Reverse for 'polls/poll_results' with arguments '(1L,)' and keyword arguments '{}' not found.
My code was working perfectly before I attempted the generic views, but I can't seem pinpoint the problem now.
Here's the code for my urls.py in the poll directory:
from django.conf.urls.defaults import *
from djtest.polls.models import Poll
info_dict = {
'queryset': Poll.objects.all(),
}
urlpatterns = patterns('',
(r'^
And here is the views.py:
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from django.core.urlresolvers import reverse
from djtest.polls.models import Poll, Choice
def vote(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
#redisplay form
return render_to_response('polls/poll_detail.html', {
'object': p,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse('poll_results', args=(p.id,)))
I have a feeling that it is a syntactical error, but I can't find it. Thanks in advance for any help...
, 'django.views.generic.list_detail.object_list', info_dict),
(r'^(?P<object_id>\d+)/
And here is the views.py:
I have a feeling that it is a syntactical error, but I can't find it. Thanks in advance for any help...
, 'django.views.generic.list_detail.object_detail', info_dict),
url(r'^(?P<object_id>\d+)/results/
And here is the views.py:
I have a feeling that it is a syntactical error, but I can't find it. Thanks in advance for any help...
, 'django.views.generic.list_detail.object_detail', dict(info_dict, template_name='polls/results.html'), 'poll_results'),
(r'^(?P<poll_id>\d+)/vote/
And here is the views.py:
I have a feeling that it is a syntactical error, but I can't find it. Thanks in advance for any help...
, 'djtest.polls.views.vote'),
)
And here is the views.py:
I have a feeling that it is a syntactical error, but I can't find it. Thanks in advance for any help...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我找不到任何解决该问题的解释,直到我遇到这个人的精简版 Django 教程: http://tony.abou-assaleh.net/web-development/stripped-down-django-tutorial
它基本上是详细信息模板中的一行,应该是:
而不是:
我不知道为什么这解决了问题,但它对我来说确实如此。 如果有人有的话,我希望得到一个解释。
I could not find any explanation that fixed the problem, until I ran across this person's abridged Django tutorial: http://tony.abou-assaleh.net/web-development/stripped-down-django-tutorial
It's basically a line in the details template, which should be:
Instead of:
I'm not sure why this fixed the issue, but it did for me. I'd love an explanation if anyone has one.
我已经尝试过作为答案提供的解决方案,但对我不起作用。 就我而言,我遇到了相同的错误(遵循相同的教程),问题是 urls.py 文件中的视图名称与views.py 中的视图名称有点不同(因为输入错误)。
I've tried the solution provided as answer and didn't worked for me. In my case i was getting the same error (following the same tutorial) and the problem was that the name of the view in the urls.py file was a bit different that in the views.py (because a typing error).
尝试使用:
Try using:
您确定这就是您的错误所在吗? 根据错误消息,听起来像是在视图或模板中尝试反转
'polls/poll_results'
(在模板中,您可能会执行类似{% url polls/poll_results poll.pk %}
)Are you sure that's where your error really is? Based on the error message, it sounds like either in a view or in a template you are trying to reverse
'polls/poll_results'
(in a template, you may be doing something like{% url polls/poll_results poll.pk %}
)