Skip to main content
 首页 » 编程设计

python之如何在运行 set_unusable_password 后对用户进行身份验证

2024年10月25日12Free-Thinker

我创建了一个用户

 new_user = create_user( 
            username=username, email=email, first_name=first_name, 
            last_name=last_name) 
new_user.set_unusable_password() 

但是 authenticate() 为该用户返回 None

auth_user = authenticate( 
            username=new_user.username, password=new_user.password) 

该用户登录失败

AttributeError: "'AnonymousUser' object has no attribute 'backend'" 

我应该如何对这些用户进行身份验证?

请您参考如下方法:

尝试编写不需要密码的自定义身份验证后端。

尝试 this :

然后您可以简单地使用您的自定义身份验证功能:

auth_user = authenticate(username=new_user.username)