繁体   English   中英

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

[英]I am getting the error that the local variable cruty referenced before assignment

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

条件,但返回不会显示Cruty,因此您可以在该范围内定义变量,这将起作用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM