更新时间:2023-12-02 08:53:40
我认为问题出在您的 EmailAuthBackend
中。如果您在后端添加一些打印/日志记录,则会发现登录表单使用用户名
和密码
调用身份验证方法。这意味着电子邮件
为无
,因此 user = User.objects.get(电子邮件=电子邮件)
查找失败。
I think the problem is in your EmailAuthBackend
. If you add some printing/logging to the backend, you'll find that the login form calls the authenticate method with username
and password
. This means that email
is None
, and therefore the user = User.objects.get(email = email)
lookup fails.
在您的情况下,常规 ModelBackend
可以正常工作,因为您有 USERNAME_FIELD ='email'
。如果您从设置中删除 AUTHENTICATION_BACKENDS
,则该登录应该有效。然后,您可以删除您的 EmailAuthBackend
。
In your case, the regular ModelBackend
will work fine for you, because you have USERNAME_FIELD = 'email'
. If you remove AUTHENTICATION_BACKENDS
from your settings then the login should work. You can then remove your EmailAuthBackend
.
如果您想使用其手机号和密码(和 cell_number
不是 USERNAME_FIELD
,那么您将需要自定义身份验证后端。还需要一个名为 authenticate(cell_number = cell_number,password = password)
的自定义身份验证表单,另一个支持自定义身份验证的示例是 RemoteUserBackend
,它根据服务器设置的环境变量登录用户。
If you wanted to log in users with their cell number and password (and cell_number
was not the USERNAME_FIELD
, then you would need a custom authentication backend. You would also need a custom authentication form that called authenticate(cell_number=cell_number, password=password)
. Another example of a custom authentication backed is RemoteUserBackend
, which logs in the user based on an environment variable set by the server.