简体   繁体   中英

python django user not authenticated with user object

Ok I am facing this problem with authentication

user_profile = UserProfile.objects.get(email="some@email.com")
new_password = form.cleaned_data['new_password']
user_profile.user.set_password(new_password)
user_profile.user.save()
user = authenticate(username=user_profile.user.username, password=new_password)
print(user)

When I print user it gives None

Why the user is not being authenticated?

The authenticate function from django.contrib.auth returns an instance of django.contrib.auth.models.User class if the credentials match or None if they don't.

I guess no user with username and password provided by you exists. That is why you are getting None.

I suggest going to the shell and running:

from django.contrib.auth.models import User
from django.contrib.auth.hashers import make_password
password = make_password("your_password")
user = User.objects.filter(username="username",password=password)

and see what user holds.

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