django-重写登录认证(可以使用用户名或手机号登录)
1、首先在setting.py文件整增加
AUTHENTICATION_BACKENDS =('study.views.CustomBackend',)
2、重写代码
from django.contrib.auth.backends import ModelBackend from django.db.models import Q from .models import UserInfo class CustomBackend(ModelBackend): def authenticate(self, request, username=None, password=None, **kwargs): try: user = UserInfo.objects.get(Q(username=username)|Q(mobile=username)) if user.check_password(password): return user except Exception as e: return None