Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Guys. I am facing some problem.

Facing the issue
Quote:
local variable 'username' referenced before assignment


Please help me.

What I have tried:

I have tried this code.

def dashboard(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = auth.authenticate(username=username, password=password)

    user_complaints = Complaint.objects.filter(username=username)
    user_applications = Application.objects.order_by('-date')
    context = {
        'complaints': user_complaints,
        'applications': user_applications
    }
    return render(request, 'accounts/dashboard.html', context)
Posted
Updated 25-Oct-20 20:15pm

And what if request.method is not 'POST'?
What values do username, password, and user contain then?

That's what the error message means: you are trying to use a variable value, but you have never assigned a value to it.
 
Share this answer
 
The Unboundlocalerror: local variable referenced before assignment is raised when you try to use a variable before it has been assigned in the local context. Python doesn't have variable declarations , so it has to figure out the scope of variables itself. It does so by a simple rule: If there is an assignment to a variable inside a function, that variable is considered local . To solve this problem, you can explicitly say it's a global by putting global declaration in your function. The global statement does not have to be at the beginning of the function definition, but that is where it is usually placed. Wherever it is placed, the global declaration makes a variable to global variable everywhere in the function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900