自定义验证规则导致Django Admin/Xadmin无法登陆

2020-12-07  本文已影响0人  和平四季豆

在学习别人的袋面的时候,抄了下面一段代码:

class CustomBackend(ModelBackend):
    """
    自定义用户验证规则
    """

    def authenticate(self, username=None, password=None, **kwargs):
        try:
            user = User.objects.get(
                Q(username=username) | Q(mobile=username))
            # django的后台中密码加密:所以不能password==password
            # UserProfile继承的AbstractUser中有def check_password(self,
            # raw_password):
            if user.check_password(password):
                return user
        except Exception as e:
            return None

然后在settings.py中配置:

 AUTHENTICATION_BACKENDS = (
     'user.views.CustomBackend',
 )

再然后就登陆一直抱用户名或密码错误:


image.png

检查了许久,才发现,登录验证被修改了(之前代码抄过来就忘了(灬ꈍ ꈍ灬),教训……)
找到问题就好修改了,要不直接把上面的取消,要不把验证中的内容修改一下

上一篇 下一篇

猜你喜欢

热点阅读