django 的身份验证不起作用
authenticate of django doesnt work
我正在为 django 重置密码,但无论我尝试什么重置都不起作用。所以我检查了我的表单处理的数据,我知道这些数据必须是真实的。它仍然没有用。所以我最近尝试在 django shell 中进行身份验证。这就是发生的事情。
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
有人知道是什么原因造成的吗?
您应该保存您的用户对象,
user.save()
根据文档,未保存用户对象:
"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
我正在为 django 重置密码,但无论我尝试什么重置都不起作用。所以我检查了我的表单处理的数据,我知道这些数据必须是真实的。它仍然没有用。所以我最近尝试在 django shell 中进行身份验证。这就是发生的事情。
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
有人知道是什么原因造成的吗?
您应该保存您的用户对象,
user.save()
根据文档,未保存用户对象:
"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