Python Django教程。柜台上没有零钱

Python Django教程。柜台上没有零钱,python,django,Python,Django,你好!关于Django的教程文档有一个问题 我对计数器的选择不响应投票结果,按下按钮会显示一条信息“投票您未选择选项”。 而且声音不带,我不明白里面有什么代码错误,请告诉我 据我所知,所有人都会处理异常 文件views.py: from django.shortcuts import get_object_or_404, render from django.http import HttpResponseRedirect, HttpResponse from djang

你好!关于Django的教程文档有一个问题 我对计数器的选择不响应投票结果,按下按钮会显示一条信息“投票您未选择选项”。 而且声音不带,我不明白里面有什么代码错误,请告诉我

据我所知,所有人都会处理异常

文件views.py:

    from django.shortcuts import get_object_or_404, render
    from django.http import HttpResponseRedirect, HttpResponse
    from django.core.urlresolvers import reverse

    from django.views import generic

    from .models import Choice, Question

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

        def get_queryset(self):
            """Return the last five published questions."""
            return Question.objects.order_by('-pub_date')[:5]

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

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

    def vote(request, question_id):
        p = get_object_or_404(Question, pk=question_id)
        print(request.POST)
        try:
            selected_choice = p.choice_set.get(pk=request.POST['choice'])
        except (KeyError, Choice.DoesNotExist) as e:
            print(e)
            # Redisplay the question voting form.
            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,)))
文件polls/url.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'),
]
文件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_text }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>
UPD:

终端输出:

[06/Sep/2015 18:45:05] "GET /polls/3/vote/ HTTP/1.1" 200 255
[06/Sep/2015 18:45:09] "GET /polls/ HTTP/1.1" 200 130
[06/Sep/2015 18:48:25] "GET /polls/3/ HTTP/1.1" 200 200
<QueryDict: {'csrfmiddlewaretoken': ['8L4m4ziCq9k9jRQzFeoCtJ8NLjq1qn8j']}>
"'choice'"
[06/Sep/2015 18:48:26] "POST /polls/3/vote/ HTTP/1.1" 200 255
<QueryDict: {'csrfmiddlewaretoken': ['8L4m4ziCq9k9jRQzFeoCtJ8NLjq1qn8j']}>
"'choice'"
[06/Sep/2015 18:48:53] "POST /polls/3/vote/ HTTP/1.1" 200 255
UPD2:

查看来源:


这是因为你在这个问题上没有任何选择。可能您没有执行第2部分中的完整示例代码


请仔细检查您的polls/detail.html文件是否与教程中的文件完全相同。不管怎样,也请把它贴在这里。我不能马上发现问题。为了帮助调试,您可以在try:行之前添加printrequest.POST,并让我们知道打印的内容。您还可以将except行更改为except KeyError,Choice.DOESNOTEXTIST为e:,然后在下一行中添加printe。无。同样,您没有选择一个选项。上面的投票按钮当我说“让我们知道打印的内容”时,我的意思是在您完成的终端中。/manage.py runserver。我希望printrequest.POST会显示一些内容-即使它是空的,您也会看到{}。如果您显示详细信息模板的呈现html,也可能会有所帮助,您可以通过在浏览器中选择类似“查看页面源”的内容来获得此信息。
<h1>Test1</h1>

<p><strong>You didn&#39;t select a choice.</strong></p>

<form action="/polls/3/vote/" method="post">
<input type='hidden' name='csrfmiddlewaretoken' value='8L4m4ziCq9k9jRQzFeoCtJ8NLjq1qn8j' />

<input type="submit" value="Vote" />
</form>