python - 类型错误 : 'bool' object is not callable g. user.is_authenticated()

标签 python flask flask-login

<分区>

我正在尝试在我的 Flask 应用程序中执行此操作。但是我收到这样的错误

TypeError: 'bool' object is not callable. 

对应的代码如下:

@app.before_request
def before_request():
    g.user = current_user
    if g.user.is_authenticated():
       g.search_form = None

最佳答案

尝试将 if g.user.is_authenticated(): 替换为 if g.user.is_authenticated:,如下所示:

@app.before_request
def before_request():
    g.user = current_user
    if g.user.is_authenticated:
       g.search_form = None

来自 the document :

is_authenticated

Returns True if the user is authenticated, i.e. they have provided valid credentials. (Only authenticated users will fulfill the criteria of login_required.)

如文档所述,is_authenticated 是一个 bool 值(TrueFalse)。

然而,它在过去是一个函数but it has been changed to boolean at version 3.0 :

BREAKING:
The is_authenticated, is_active, and is_anonymous
members of the user class are now properties, not methods. Applications should update their user classes accordingly.

关于python - 类型错误 : 'bool' object is not callable g. user.is_authenticated(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33062109/

相关文章:

python - 如何覆盖 __init__ 同时保存继承自 OrderedDict 的旧 __init__

python - 如何将用户对象传递给 Django 中的表单

python - 如何捕获一系列 Celery 任务执行过程中产生的所有 python 日志记录?

python - Flask Sqlalchemy中如何分离主从(DB读/写)

python - 使用 Rabbitmq 和 Python 进行 Stomp 广播

python - celery-beat KeyError : 'scheduler'

python - 为什么我的 alembic 迁移有问题

python - 带有 Python 请求包的 Flask 服务器上的 session 不起作用

python - 装饰Flask MethodView派生类中的方法

python - 如何手动安装 Flask 扩展?