简体   繁体   中英

Local variable referenced before assignment - Django 2.1

I´ve been looking for ways to authenticate an user without password in Django, but that is not my doubt.

I´ve seen this answer which has 24 upvotes. The answer says I have do that in my views.py:

user = authenticate(username=user.username)
login(request, user)

When I execute the code, I´m getting this error: UnboundLocalError: local variable 'user' referenced before assignment . This is obvious for me, I´m defining a variable using that variable.

So, why does the answer have 24 upvotes if it isn´t working? What do I have to do to get above code working?

Django version: 2.1

As @chepner explained in simple words, the issue here is to do with the use of it in the local scope.

If you followed those steps correctly, in the relevant function in views.py , you need to add the aforementioned lines of code. The user would be your User Object that you are trying to authenticate without the password.

You can see more about the User object here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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