我有此代码,但显示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,)))
答案 0 :(得分:3)
return语句是函数专有的。
您应将class vote
更改为def vote