how-to-make-django-auth-with-both-email-and-username

做一个小小的记录 扩展认证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from django.contrib.auth.backends import ModelBackend
from django.db.models import Q
from .models import UserProfile


class CustomBackend(ModelBackend):
"""邮箱也能登录"""
def authenticate(self, request, username=None, password=None, **kwargs):
try:
user=UserProfile.objects.get(Q(username=username)|Q(email=username))
if user.check_password(password):
return user
except Exception as e:
return None