Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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')) 
like image 207
Gaoyang Avatar asked Oct 07 '15 03:10

Gaoyang


People also ask

How do I fix bool is not callable?

The Python "TypeError: 'bool' object is not callable" occurs when we try to call a boolean value ( True or False ) as a function. To solve the error, correct the assignment and resolve any clashes between function and variable names.

Why is the bool not callable in Python?

Bool type objects do not respond to a function call because they are not functions. If you try to call a Bool type object if it were a function, the Python interpreter will raise the TypeError: 'bool' object is not callable.


1 Answers

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

like image 79
Sepehr Hamzehlooy Avatar answered Oct 13 '22 12:10

Sepehr Hamzehlooy