python Django 中的 NoReverseMatch 异常帮助
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1204138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NoReverseMatch Exception help in Django
提问by mportiz08
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:
我对 python 还很陌生,并在此处阅读了 Django 框架教程的第 4 部分。我正在尝试为投票应用程序实现通用视图——我的代码似乎是正确的(据我所知),但是当我尝试投票时,我收到一个 NoReverseMatch 异常,指出:
Reverse for 'polls/poll_results' with arguments '(1L,)' and keyword arguments '{}' not found.
使用参数“(1L,)”和关键字参数“{}”未找到的“polls/poll_results”反转。
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:
这是我的 urls.py 在 poll 目录中的代码:
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'),
)
And here is the views.py:
这是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...
我有一种感觉,这是一个语法错误,但我找不到它。在此先感谢您的帮助...
采纳答案by bstpierre
Try using:
尝试使用:
return HttpResponseRedirect(reverse('poll_results', kwargs={'object_id': p.id}))
回答by SmileyChris
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 %}
)
你确定这就是你的错误所在吗?根据错误消息,听起来像是在视图中或在您尝试反转'polls/poll_results'
的模板中(在模板中,您可能正在执行类似的操作{% url polls/poll_results poll.pk %}
)
回答by Syam Palakurthy
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
我找不到任何解决问题的解释,直到我遇到了这个人的删节 Django 教程:http: //tony.abou-assaleh.net/web-development/stripped-down-django-tutorial
It's basically a line in the details template, which should be:
它基本上是详细信息模板中的一行,应该是:
<form action="/polls/{{ poll.id }}/vote/" method="post">
Instead of:
代替:
<form action="{% url 'polls.views.vote' poll.id %}" method="post">
I'm not sure why this fixed the issue, but it did for me. I'd love an explanation if anyone has one.
我不确定为什么这解决了这个问题,但对我来说确实如此。如果有人有解释,我希望得到解释。
回答by Santiago
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).
我已经尝试了作为答案提供的解决方案,但对我不起作用。在我的情况下,我遇到了相同的错误(遵循相同的教程),问题是 urls.py 文件中的视图名称与 views.py 中的视图名称略有不同(因为输入错误)。