字段 'id' 需要一个数字但得到了 'on'

Field 'id' expected a number but got 'on'

我正在做一个包含在 Django 文档中的项目。

当我输入 URL http://localhost:8000/polls/1/vote/ 时,我会在下面一行得到错误:

invalid literal for int() with base 10: 'on'

下面几行是我的视图定义:

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):
        return render(request, 'polls/detail.html', {
            'question': question,
            'error_message': "You didn't select a choice",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

您已在 post 消息中发送了值 "on" 并带有字段 request.POST["choice"] ,这对于需要数字 [=] 的 selected_choice = question.choice_set.get(pk=request.POST['choice']) 来说不是有效的 pk 13=]