Django投票无法统计结果,如何解决?

问题遇到的现象和发生背景

Python 3.10
Django version 4.0.6
在Django教程第一步的投票教程,
点了vote后就会报错

问题相关代码,请勿粘贴截图

from django.shortcuts import render

Create your views here.

from django.http import HttpResponse,HttpResponseRedirect
from .models import Question,Choice
from django.template import loader
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.urls import reverse

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,selected_choice.DoesNotExist):
return render(request,'polls/detail.html',{
'question':question,
'error_message':"还没有选择任何选项",
})
else:
selected_choice.votes+=1
selected_choice.save()
return HttpResponseRedirect(reverse('polls:results',args=(question.id,)这里还有几个括号我删了不然发不出来代码

运行结果及报错内容

UnboundLocalError: local variable 'selected_choice' referenced before assignment

我的解答思路和尝试过的方法

应该不是重名的关系,因为我把selected_choice改为selected_choice1还是会报同样的错误

我想要达到的结果

我看书上都没有什么错误,我一直一步一步按照书上来的。

我估计是哪个地方写错了,又重写了一遍就好了