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 に答える