Django2.X兼容 集成user.is_authenticated

使用django自带的认证系统, 挪移到django2.X启动服务后报错:

TypeError at /index

'bool' object is not callable

错误代码:

if user.is_authenticated():


解决方法,取消调用:

if user.is_authenticated:

---------------------------------

原因分析:

此集成模块的is_authenticated方法指向models.auth模块中的用户认证中AbstractBaseUser和AnonymousUser,这两个类中的同名方法.
登录前, AnonymousUser的方法调用后返回False;登录后, 则由AbstractBaseUser返回True, 以此来判断用户是否登录.

新版本中, 两个类中的同名方法使用@property装饰, 引用时直接由所属类返回属性.无需调用.


源码:

    @property
    def is_authenticated(self):
        """
        Always return True. This is a way to tell if the user has been
        authenticated in templates.
        """
        return True



原文地址:https://www.cnblogs.com/jrri/p/11610673.html