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