【问题标题】:NoReverseMatch Django Tutorial 1.8NoReverseMatch Django 教程 1.8
【发布时间】:2015-06-15 23:29:53
【问题描述】:

所以我从本教程的通用视图部分开始,直到此时一切都一帆风顺,工作完美,然后我得到了这个错误: 未找到带有参数 '('',)' 和关键字参数 '{}' 的 'vote' 的反向操作。尝试了 1 种模式:[u'polls/(?P[0-9]+)/vote/$']

这是我的 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>

【问题讨论】:

    标签: python django


    【解决方案1】:

    你有两个问题。第一个是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>
    

    【讨论】:

    • 是的,这行得通。谢谢。看起来我在模型中为问题所做的错字是主要问题。
    • 哈 - 我什至没有注意到。
    猜你喜欢
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 2015-06-21
    • 2021-02-14
    • 2016-12-24
    • 2018-11-02
    • 2017-11-26
    相关资源
    最近更新 更多