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

Antonio Sanchez
  • 381
  • 1
  • 3
  • 11
  • 1
    It works fine at the global scope, because you aren't introducing a new local variable named `user` that shadows the global that `user.username` expects to use. – chepner Dec 30 '20 at 21:40

1 Answers1

0

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.

AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35