我正在做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,)))
您已在带有字段
request.POST["choice"]
的post消息中发送了值"on"
,该字段不是需要数字的selected_choice = question.choice_set.get(pk=request.POST['choice'])
的有效pk相关问题 更多 >
编程相关推荐