21

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'))
davidism
  • 98,508
  • 22
  • 317
  • 288
Gaoyang
  • 221
  • 1
  • 2
  • 5

2 Answers2

51

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

11

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.

Peter
  • 8,517
  • 4
  • 37
  • 63