• 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



  • 相关阅读:
    基础
    树梅派线程
    超声波
    电脑版微信双开多开
    子类能不能重写父类的构造方法
    window8taskost.exe一直占用cpu
    windows下rocketmq安装
    spring循环依赖问题
    线程池的种类
    并行和并发有什么区别?
  • 原文地址:https://www.cnblogs.com/jrri/p/11610673.html
Copyright © 2020-2023  润新知