Django教程第4章中的NoReverseMatch

时间:2015-04-15 11:39:21

标签: django

我的Django演示项目中出现以下错误

NoReverseMatch at /polls/4/vote/
Reverse for 'results' with arguments '(4,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: POST
Request URL:    http://127.0.0.1:8000/polls/4/vote/
Django Version: 1.8
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'results' with arguments '(4,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Exception Location: C:\Python34\lib\site-packages\django-1.8-py3.4.egg\django\core\urlresolvers.py in _reverse_with_prefix, line 496
Python Executable:  C:\Python34\python.exe
Python Version: 3.4.3
Python Path:    
['D:\\DjangoDemo\\demo',
 'C:\\Python34\\lib\\site-packages\\django-1.8-py3.4.egg',
 'C:\\Python34\\lib\\site-packages\\setuptools-15.0-py3.4.egg',
 'C:\\windows\\system32\\python34.zip',
 'C:\\Python34\\DLLs',
 'C:\\Python34\\lib',
 'C:\\Python34',
 'C:\\Python34\\lib\\site-packages']
Server time:    Wed, 15 Apr 2015 17:04:09 +0530

我的观点: -

from django.utils import timezone
from django.shortcuts import render
from django.http import Http404
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.template import RequestContext,loader
from .models import Question,Choice

def index(request):

    #get latest 5 Question object from DB
    latest_question_list = Question.objects.order_by('-pub_date')[:5]

    # iterate over question object list and print question_text 
    #output = ', '.join([p.question_text for p in latest_question_list])
    #Use Django Template loader and load index.html template
    template = loader.get_template('polls/index.html')
    #context is same as requestDispatcher it take request and Dict and send to loaded Template bcoz target template hold this key value.
    context = RequestContext(request,{
    'latest_question_list':latest_question_list,
    })

    #SortCut
    #context = {'latest_question_list': latest_question_list}
    #return render(request, 'polls/index.html', context)

    return HttpResponse(template.render(context))


def detail(request,question_id):

    try:
        question = Question.objects.get(pk=question_id)
    except Question.DoesNotExist:
        raise Http404("Question does not exist")
    return render(request,'polls/detail.html',{'question':question})

def results(request,question_id):
    question = get_object_or_404(Question,pk=question_id)
    return render(request,'polls/results.html',{'question':question})

def vote(request,question_id):

    p = get_object_or_404(Question,pk=question_id)
    try:
        selected_choice = p.choice_set.get(pk=request.POST['choice'])
    except (KeyError,Choice.DoesNotExist):
        return render(request, 'polls/detail.html',{

        'question':p,
        'error_message':"You didn't select a choice",
        })
    else:
        selected_choice.votes +=1
        selected_choice.save()
        return HttpResponseRedirect(reverse('polls:results',args=(p.id,)))

详细信息页面: -

<h1>{{ question.question_text }}</h1>

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %}
    <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
    <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>

轮询/ urls.py:

from django.conf.urls import url
from . import views

urlpatterns = [

    url(r'^$', views.index,name='index'),
    url(r'^(?P<question_id>[0-9]+)/$', views.detail,name='detail'),
    url(r'^(?P<question_id>[0-9]+)/results/$', views.results,name='result'),
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote,name='vote'),
]

1 个答案:

答案 0 :(得分:0)

在您的urls.py中,您的网址名称为result。它应该是results以匹配您的视图和教程。