0

嗨,我在 django 网站上关注本教程。我想我现在已经复制粘贴了所有内容。但是出现错误"NoReverseMatch at /polls/2/vote/""Reverse for 'views.results' with arguments '(2,)' and keyword arguments '{}' not found."查看了有关此主题的其他一些帖子,但没有发现它们的帮助。即添加name='views'或类似的东西。

我的urls.py

from django.conf.urls.defaults import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('mysite.polls.views',
    url(r'^$', 'index'),
    url(r'^polls/$', 'index'),
    url(r'^polls/(?P<poll_id>\d+)/$', 'detail'),
    url(r'^polls/(?P<poll_id>\d+)/results/$', 'results', name='polls.views.results'),
    url(r'^polls/(?P<poll_id>\d+)/vote/$', 'vote'),
    url(r'^admin/', include(admin.site.urls)),
)
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

我的views.py

from django.shortcuts import get_object_or_404, render_to_response from django.http import HttpResponseRedirect, HttpResponse from django.core.urlresolvers import reverse from django.template import RequestContext from models import Choice, Poll

def index(request):
    latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
    return render_to_response('indexx.html', {'latest_poll_list': latest_poll_list},) def detail(request, poll_id):
    try:
        p = Poll.objects.get(pk=poll_id)
    except Poll.DoesNotExist:
        raise Http404
    return render_to_response('detail.html', {'poll': p}, context_instance=RequestContext(request))

def results(request, poll_id):
    p = get_object_or_404(Poll, pk=poll_id)
    return render_to_response('results.html', {'poll': p})

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 the poll voting form.
        return render_to_response('detail.html', {
            'poll': p,
            'error_message': "You didn't select a choice.",
        }, context_instance=RequestContext(request))
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse('views.results', args=[p.id]))
4

1 回答 1

1

url 的名称polls.views.results不是views.results。它应该是:

return HttpResponseRedirect(reverse('polls.views.results', args=[p.id]))
于 2013-01-20T14:06:10.000 回答