一:指定后端
AUTHENTICATION_BACKENDS = (
'authentication.authentication.CustomAuthenticationBackend',
)
二:自定义后端
from django.contrib.auth import get_user_model
class CustomAuthenticationBackend:
def authenticate(self, email, password):
User = get_user_model()
try:
user = User.objects.get(email=email)
if user.check_password(password):
return user
except User.DoesNotExist:
return None
def get_user(self, email):
User = get_user_model()
try:
return User.objects.get(email)
except User.DoesNotExist:
return None