is_authenticated() raises TypeError TypeError: 'bool' object is not callable

is_authenticated() raises TypeError TypeError: 'bool' object is not callable

我试图在视图中使用 is_authenticated(),但收到错误 `TypeError: 'bool' object is not callable。为什么会出现此错误以及如何解决?

@auth.before_app_request
def before_request():
    if current_user.is_authenticated() \
            and not current_user.confirmed \
            and request.endpoint[:5] != 'auth.' \
            and request.endpoint != 'static':
        return redirect(url_for('auth.unconfirmed'))

Flask-Login 0.3.0(2015 年 9 月 10 日发布)更改:

  • 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.

因此您需要更改 user class 并相应地编写代码。

"object is not callable" 当您尝试将对象当作方法或函数时会发生错误。

在这种情况下:

current_user.is_authenticated()

您的行为 current_user.is_authenticated 是一种方法,但它不是一种方法。

你必须这样使用它:

current_user.is_authenticated

您在方法或函数之后使用“( )”,而不是对象。

在某些情况下,class 可能会实现 __call__ 函数,您也可以调用对象,然后它就可以调用了。