字段“id”需要一个数字,但得到了“on”

2024-09-28 21:27:58 发布

您现在位置:Python中文网/ 问答频道 /正文

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

当我输入URLhttp://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,)))


Tags: 项目django文档idlocalhostgetreturnrequest
1条回答
网友
1楼 · 发布于 2024-09-28 21:27:58

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

相关问题 更多 >