我收到赋值之前局部变量Cruty引用的错误

时间:2019-02-10 09:59:09

标签: python django

获取分配前引用的错误局部变量“ 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})

1 个答案:

答案 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})

始终确保您正在访问的变量在代码块的范围内,例如您正在尝试访问

外部的Cruty
if fname is not None

条件,但返回的粗暴性不可见,因此您可以在该范围内定义变量,这将起作用