dajngo2.1用户名和密码都正确authticate返回None

django2.1后默认的认证后端(django.contrib.auth.backends.ModelBackend)
而这个类中的authenticate方法中有如下代码:

成都服务器托管,创新互联建站提供包括服务器租用、成都二枢机房、带宽租用、云主机、机柜租用、主机租用托管、CDN网站加速、国际域名空间等业务的一体化完整服务。电话咨询:13518219792

    def authenticate(self, request, username=None, password=None, **kwargs):
            .....
        if user.check_password(password) and self.user_can_authenticate(user):
                return user

而且user_can_authenticate函数如下:

    def user_can_authenticate(self, user):
        is_active = getattr(user, 'is_active', None)
        return is_active or is_active is None

由此可知django2.1后也会对用户的is_active进行判断,如果is_active为false,则authticate也会返回None.

要解决以上问题,只要继承django.contrib.auth.backends.ModelBackend类重写authticate方法返回True即可,此出刚好有一个类实现了以上操作django.contrib.auth.backends.AllowAllUsersModelBackend.
在settings.py中重新指定后端处理类即可,如下:

AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.AllowAllUsersModelBackend']

网页名称:dajngo2.1用户名和密码都正确authticate返回None
文章起源:http://myzitong.com/article/ggiops.html