我想同时登录username
和email
的页面,无论如何更改,我都写了一个CustomBackend
视图进行身份验证,只有username
有效,我使用“电子邮件”登录页面,总是出现错误Unable to log in with provided credentials
,views.py的部分代码如下:
from django.contrib.auth import get_user_model
MyUser = get_user_model()
class CustomBackend(object):
def authenticate(self, username=None, password=None, **kwargs):
try:
user = MyUser.objects.get(Q(username=username)|Q(email=username))
if user.check_password(password):
return user
except MyUser.DoesNotExist:
MyUser().set_password(password)
settings.py的部分代码,如下所示:
AUTHENTICATION_BACKENDS = (
'general.views.CustomBackend',
'django.contrib.auth.backends.ModelBackend'
)
任何想法都会受到高度赞赏。