问题
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, andis_anonymousmembers 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