在setting内增加
# Application definition
AUTHENTICATION_BACKENDS = (
'users.views.CustomBackend',
)
在views下重写authenticate方法,让支持邮箱和手机登陆
from django.contrib.auth.backends import ModelBackend
from django.db.models import Q
class CustomBackend(ModelBackend):
def authenticate(self, username=None, password=None, **kwargs):
try:
user = UserProfile.objects.get(Q(username=username) | Q(email=username) | Q(mobile=username))
if user.check_password(password):
return user
except Exception as e:
return None
然后就可以实现数据库中的手机和邮箱也能登陆了,不用仅限用户名了