django自定义用户认证模块

class CustomAuth(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        try:
            user = UserProfile.objects.get(Q(username=username) | Q(mobile=username))
            if user.check_password(password):
                return user
        except Exception as e:
            return None

  

AUTHENTICATION_BACKENDS = [
    'apps.users.views.CustomAuth',   # 引用自定义的认证后端
]

  

 

官方文档:https://docs.djangoproject.com/en/2.2/topics/auth/customizing/

 

posted @ 2020-05-01 14:37  本人小白  阅读(182)  评论(0)    收藏  举报