如何在Django中解决外部返回函数

时间:2019-01-10 11:38:11

标签: django try-except

我有此代码,但显示return outside function错误

我做错什么了吗?我该如何解决?

class 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,)))

1 个答案:

答案 0 :(得分:3)

return语句是函数专有的。

您应将class vote更改为def vote