字段'id'希望有一个数字,但已接通'

问题描述 投票:0回答:2

我正在做Django文档中包含的项目。当我输入URL- http://localhost:8000/polls/1/vote/时,在下面的行中收到错误,

def vote(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice = question.choice_set.get(pk=request.POST['choice'])
    except(KeyError, Choice.DoesNotExist):
        # redisplay the question voting form
        return render(request, 'polls/detail.html', {
            'question': question,
            'error_message': "You didn't select a choice",
        })
    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('polls:results', args=(question.id,)))

python django python-3.6 web-frameworks
2个回答
0
投票

您已经在具有"on"字段的帖子中发送了request.POST["choice"]值,这对于selected_choice = question.choice_set.get(pk=request.POST['choice'])来说是无效的pk,需要一个数字

© www.soinside.com 2019 - 2024. All rights reserved.