使用参数'(1,)'和关键字参数'{}'找不到'views.results'

时间:2013-01-20 13:59:49

标签: django django-views

嗨我跟随django网站上的this tutorial。我想我现在已经复制粘贴了所有内容。但是收到错误"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]))

1 个答案:

答案 0 :(得分:1)

网址名称为polls.views.results而不是views.results。它应该是:

return HttpResponseRedirect(reverse('polls.views.results', args=[p.id]))