简体   繁体   中英

authenticate of django doesnt work

I am working on a password reset for django, but whatever I try the reset doesn't work. So i checked what my form handled on data which i knew had to be true. It still didn't work. So alst i tried to authenticate in the django shell. and this is what happened.

shell:

In [11]: user = User.objects.first()
In [12]: password = "bier"
In [13]: user.set_password(password)
In [14]: i = authenticate(username=user.username, password=password)
In [15]: i
i returns None

Someone any clue about what is causing this?

You should save your user object,

user.save()

According to the docs, user object is not saved:

"Sets the user's password to the given raw string, taking care of the password hashing. Doesn't save the User object."

https://docs.djangoproject.com/en/1.8/ref/contrib/auth/#django.contrib.auth.models.User.set_password

from django.contrib.auth.models import User
u = User.objects.get(username='john')
u.set_password('new password')
u.save()

https://docs.djangoproject.com/en/dev/topics/auth/default/#changing-passwords https://docs.djangoproject.com/en/dev/topics/auth/default/#authenticating-users

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