【发布时间】:2020-05-08 05:24:55
【问题描述】:
我正在做一个包含在 Django 文档中的项目。
当我输入 URL http://localhost:8000/polls/1/vote/ 时,我会在下面一行得到错误:
以 10 为底的 int() 的无效文字:'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 请求吗?请给出完整的请求/错误/响应!
-
能否也显示错误信息?
-
selected_choice = question.choice_set.get(pk=request.POST['choice']) -
print(request.POST.get('choice'))...打印是您拥有的最简单的调试形式...我的猜测是该值是“on”... pk 需要一个数字 -
**上述异常(invalid literal for int() with base 10: 'on')是以下异常的直接原因:**
标签: python django python-3.6 web-frameworks