获取分配前引用的错误局部变量“ cruty”
def index(request):
if request.method =="POST":
fname= request.POST.get("fname",None)
if fname is not None:
dname= Students.objects.get(USN=fname)
print(dname)
cruty= Marks.objects.filter(students=dname)
print(cruty)
return render(request, "index.html", {"cruty": cruty})
答案 0 :(得分:0)
def index(request):
if request.method =="POST":
cruty = None
fname = request.POST.get("fname",None)
if fname is not None:
dname= Students.objects.get(USN=fname)
print(dname)
cruty= Marks.objects.filter(students=dname)
print(cruty)
return render(request, "index.html", {"cruty": cruty})
始终确保您正在访问的变量在代码块的范围内,例如您正在尝试访问
外部的Crutyif fname is not None
条件,但返回的粗暴性不可见,因此您可以在该范围内定义变量,这将起作用