Flask View Raises Typeerror: 'Bool' Object Is Not Callable

Flask view raises TypeError: 'bool' object is not callable

In Flask, a view must return one of the following:

  • a string
  • a Response object (or subclass)
  • a tuple of (string, status, headers) or (string, status)
  • a valid WSGI application

Flask tests for the first 3 options, and if they don't fit, assumes it is the fourth. You returned True somewhere, and it is being treated as a WSGI application instead.

See About Responses in the documentation.

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.

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

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.

Flask Python - int is not callable

This is answered here, but in short, a response can be a string, but not int.

The documentation:

  1. If a response object of the correct type is returned it’s directly returned from the view.
  2. If it’s a string, a response object is created with that data and the default parameters.
  3. If a tuple is returned the items in the tuple can provide extra information. Such tuples have to be in the form (response, status, headers) or (response, headers) where at least one item has to be in the tuple. The status value will override the status code and headers can be a list or dictionary of additional header values.
  4. If none of that works, Flask will assume the return value is a valid WSGI application and convert that into a response object.

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.

Why am I getting TypeError in the Flask code?

from flask import Flask, render_template
import urllib.request
import json
import time

app = Flask(__name__ ,template_folder='template')
namep = "PewDiePie"
namet = "TSeries"
key = "MY_API_KEY"

@app.route("/")
def function_main():
datat = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername="+namep+"&key="+key).read()
datap = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername="+namet+"&key="+key).read()
subt = json.loads(datat)["items"][0]["statistics"]["subscriberCount"]
subsp = json.loads(datap)["items"][0]["statistics"]["subscriberCount"]
return render_template('template\index.html', subsp = subsp, subt = subt)#Sends the integers to index.html to get printed in the Flask server

if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=80)


Related Topics



Leave a reply



Submit