Django 中的 NoReverseMatch 异常帮助

发布于 2024-07-29 06:53:26 字数 1849 浏览 12 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜雨飘雪 2024-08-05 06:53:27

我找不到任何解决该问题的解释,直到我遇到这个人的精简版 Django 教程: http://tony.abou-assaleh.net/web-development/stripped-down-django-tutorial

它基本上是详细信息模板中的一行,应该是:

<form action="/polls/{{ poll.id }}/vote/" method="post">

而不是:

<form action="{% url 'polls.views.vote' poll.id %}" method="post">

我不知道为什么这解决了问题,但它对我来说确实如此。 如果有人有的话,我希望得到一个解释。

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:

<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.

晨曦慕雪 2024-08-05 06:53:27

我已经尝试过作为答案提供的解决方案,但对我不起作用。 就我而言,我遇到了相同的错误(遵循相同的教程),问题是 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).

勿挽旧人 2024-08-05 06:53:26

尝试使用:

return HttpResponseRedirect(reverse('poll_results', kwargs={'object_id': p.id}))

Try using:

return HttpResponseRedirect(reverse('poll_results', kwargs={'object_id': p.id}))
北恋 2024-08-05 06:53:26

您确定这就是您的错误所在吗? 根据错误消息,听起来像是在视图或模板中尝试反转 '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 %})

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文