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

送分小仙女□ 提交于 2019-11-27 01:38:47

问题


This question already has an answer here:

  • Flask-Login raises TypeError: 'bool' object is not callable when trying to override is_active property 2 answers

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

回答1:


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

in this case:

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.

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




回答2:


From Flask-Login 0.3.0 (released on September 10th, 2015) changes:

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

So you need to change your user class and code accordingly.



来源:https://stackoverflow.com/questions/32983133/is-authenticated-raises-typeerror-typeerror-bool-object-is-not-callable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!