NoReverseMatch Django教程1.8

时间:2015-04-10 09:41:46

标签: python django

所以我开始了教程的通用视图部分,直到哪一点一切顺利,完美运行然后我收到此错误: 反向'投票'有参数'('',)'和关键字参数' {}'未找到。尝试了1种模式:[u'民意调查/(?P [0-9] +)/投票/ $']

这是我的urls.py:

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

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

这是我的views.py:

from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext, loader 
from django.http import Http404
from .models import Choice, Quesion
from django.core.urlresolvers import reverse
from django.views import generic
# Create your views here.

class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_question_list'

    def get_queryset(self):
        return Quesion.objects.order_by('-pub_date')[:5]

class DetailView(generic.DetailView):
    model = Quesion
    template_name = 'polls/detail.html'

class ResultsView(generic.DetailView):
    model = Quesion
    template_name = 'polls/results.html'

def vote(request, question_id):
    p = get_object_or_404(Quesion, 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':"No choice selected",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        return HttpResponseRedirect(reverse('polls:results',args=(p.id,)))

这是我的detail.html

<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_txt }}   </label><br />
{% endfor %}
<input type="submit" value="vote" />
</form>

1 个答案:

答案 0 :(得分:3)

你有两个问题。第一个是DetailView未在模板中提供question变量,而是提供一个名为object的变量。因此,模板中question的所有实例都需要更改为object

其次,URL期望关键字参数question_id,但您将非关键字参数传递给{% url %}标记。您需要将其更改为question_id=object.id。您的detail.html应如下所示:

<h1>{{ object.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

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