is_authenticated() 引发 TypeError TypeError: 'bool' object is not callable [英] is_authenticated() raises TypeError TypeError: 'bool' object is not callable

查看:28
本文介绍了is_authenticated() 引发 TypeError TypeError: 'bool' object is not callable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在视图中使用 is_authenticated(),但收到错误 `TypeError: 'bool' object is not callable.为什么我会收到这个错误,我该如何解决?

I tried to use is_authenticated() in a view, but got the error `TypeError: 'bool' object is not callable. Why am I getting this error and how do I fix it?

@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'))

推荐答案

object is not callable"错误发生在当您尝试将对象作为方法或函数的行为时.

"object is not callable" error occurs when you are trying to behave an object like it is a method or function.

在这种情况下:

current_user.is_authenticated()

您将 current_user.is_authenticated 视为一种方法,但它不是一种方法.

you are behaveing current_user.is_authenticated as a method but its not a method .

你必须以这种方式使用它:

you have to use it in this way :

current_user.is_authenticated

在方法或函数后使用( )",而不是对象.

you use "( )" after methods or functions, not objects.

在某些情况下,类可能会实现 __call__ 函数,您也可以调用该函数,然后它就可以调用了.

In some cases a class might implement __call__ function which you can call an object too, then it will be callable.

这篇关于is_authenticated() 引发 TypeError TypeError: 'bool' object is not callable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆