Django auth.models.User 未经过身份验证,但存在于 auth 数据库中

标签 django django-authentication

这是一个示例,根据我目前的理解,它应该可以工作。下面的代码是直接从 ./manage.py shell

中测试复制的

auth 数据库中有一个注册用户:

from django.contrib.auth.models import User
user_in_database = User.objects.all()[0]
print user_in_database.username     #username
print user_in_database.password     #pass

认证测试:

from django.contrib.auth import authenticate
authenticated_user = authenticate(username = user_in_database.username,
                                  password = user_in_database.password)
type(authenticated_user)   #NoneType

它返回 None,我希望它从数据库返回用户。我在这里遗漏了一些重要的东西吗?

最佳答案

您缺少的是 user_in_database.password 不是用户的密码。相反,它是实际密码的哈希值(只需打印它来检查自己)。

本质上,当您调用authenticate时,您没有使用正确的密码。

因此,authentication fails, and returns None (因为这就是当你传递不正确的凭据时 django 返回的结果)。

关于Django auth.models.User 未经过身份验证,但存在于 auth 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19067768/

相关文章:

django - 如何过滤多对多模型?

django - 用于访问 Django REST 框架的 secret API key

python - Django:获取按组分隔的模板的所有权限

Django:失去理智,用 Django auth 拔掉我的头发

python - 如何在views.py中调用django自定义编写的AuthenticationBackend的authenticate()方法?

Django auth - 添加用户字段 - 在管理员中显示

Django开发服务器智能卡认证

python - 如何在 Django REST 框架上启用 CORS

Android-Django用户认证

python - Django 'Key(field)=() is duplicated'