Is_authenticated () throws TypeError TypeError: object 'bool' cannot be called

I tried using is_authenticated() in the view, but got the error `TypeError: 'bool' object is not being called. Why am I getting this error and how to 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')) 
+7
python flask flask-login
source share
2 answers

"object not callable" error occurs when you try to behave like an object, such as a method or function.

in this case:

 current_user.is_authenticated() 

you are running current_user.is_authenticated as a method, but it is not a method.

you should use it as follows:

 current_user.is_authenticated 

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

In some cases, the class may implement the __call__ function, which you can also call the object, then it will be called.

+7
source share

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

  • BREAKING: the members of 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 the class and user code accordingly.

+8
source share

All Articles