Flask-Login Raises Typeerror: 'Bool' Object Is Not Callable When Trying to Override Is_Active Property

Flask-Login raises TypeError: 'bool' object is not callable when trying to override is_active property

is_active, is_anonymous, and is_authenticated are all properties as of Flask-Login 0.3. If you want to use them, treat them as attributes, don't call them. If you want to override them, remember to decorate them with @property.

# change from
current_user.is_authenticated()
# to
current_user.is_authenticated

It appears you are reading the docs for the most recent version (0.3), but using an older version of the library. Version 0.3 contains a breaking change which changed these attributes from methods to properties. You should upgrade to the latest version of Flask-Login and treat them as properties.

You deactivate the user by causing its is_active property to return False. Your idea to return the value of a column is fine.

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

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

TypeError: 'bool' object is not callable

You do cls.isFilled = True. That overwrites the method called isFilled and replaces it with the value True. That method is now gone and you can't call it anymore. So when you try to call it again you get an error, since it's not there anymore.

The solution is use a different name for the variable than you do for the method.

TypeError: 'bool' object is not callable in flask

is_active is bool object.

>>> is_active = True
>>> is_active()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'bool' object is not callable

Just use it as a predicate, instead of calling it:

if not force and not user.is_active:
...

TypeError: 'bool' object is not callable g.user.is_authenticated()

Try replace if g.user.is_authenticated(): to if g.user.is_authenticated: like this:

@app.before_request
def before_request():
g.user = current_user
if g.user.is_authenticated:
g.search_form = None

From the document:

is_authenticated

Returns True if the user is authenticated, i.e. they have provided valid credentials. (Only authenticated users will fulfill the criteria of login_required.)

As the document said, is_authenticated is a boolean(True or False).

And however, it was a function in the past, but it has been changed to boolean at version 3.0:

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.

How to call is_authenticated in Flask-login

Like this? Check if it's a boolean, or it's a function:

if type(is_authenticated) == type(True): # if it's a boolean, this is True.
# Your code

else: # if not, it's a function.
is_authenticated()
# Your code

And create a function here is a good idea like this:

def function_name():
if type(is_authenticated) == type(True):
return is_authenticated
else:
return is_authenticated()

Nginx not returning html templates correctly when connecting via dns

File "/var/www/new-elf-lite/app/templates/base.html", line 71, in block "navbar"
{% if current_user.is_authenticated() %}
AttributeError: 'bool' object has no attribute '__call__'

current_user.is_authenticated will return True or Flase, but you call it, it is a boolean value.

the solution is simple, you should use this, with out the ():

{% if current_user.is_authenticated %}


Related Topics



Leave a reply



Submit