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

Gaoyang picture Gaoyang · Oct 7, 2015 · Viewed 35.4k times · Source

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

Answer

Sepehr Hamzehlooy picture Sepehr Hamzehlooy · Oct 7, 2015

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