The Flask view raises a TypeError: the 'bool' object is not called

I am trying to debug a view in my Flask application that returns a status of 500 with a TypeError: 'bool' object is not callable error TypeError: 'bool' object is not callable in the trace. The view calls login_user from Flask-Login, then returns True to indicate that the login was successful.

I debugged to app_iter = app(environ, start_response) , and the app now has a boolean value of True , not a Flask application object.

 Traceback (most recent call last): File "D:\Python27\lib\site-packages\flask\app.py", line 1836, in __call__ return self.wsgi_app(environ, start_response) File "D:\Python27\lib\site-packages\flask\app.py", line 1820, in wsgi_app response = self.make_response(self.handle_exception(e)) File "D:\Python27\lib\site-packages\flask\app.py", line 1403, in handle_exception reraise(exc_type, exc_value, tb) File "D:\Python27\lib\site-packages\flask\app.py", line 1817, in wsgi_app response = self.full_dispatch_request() File "D:\Python27\lib\site-packages\flask\app.py", line 1478, in full_dispatch_request response = self.make_response(rv) File "D:\Python27\lib\site-packages\flask\app.py", line 1577, in make_response rv = self.response_class.force_type(rv, request.environ) File "D:\Python27\lib\site-packages\werkzeug\wrappers.py", line 824, in force_type response = BaseResponse(*_run_wsgi_app(response, environ)) File "D:\Python27\lib\site-packages\werkzeug\wrappers.py", line 57, in _run_wsgi_app return _run_wsgi_app(*args) File "D:\Python27\lib\site-packages\werkzeug\test.py", line 854, in run_wsgi_app app_iter = app(environ, start_response) TypeError: 'bool' object is not callable 
 @app.route('/login', methods=['POST']) def login(): username = request.form['username'] user = User.query.filter_by(username=username).first() if user: login_user(user) return True return False 
+9
python flask flask-login
source share
2 answers

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

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

Flask tests for the first 3 options, and if they do not fit, suggests that this is the fourth. You returned True somewhere, and instead, it is treated as a WSGI application.

See About answers in the documentation.

+26
source share

This is the answer to the "duplicate" question, which is here: What is the checkbox answer to use the variables from the HTML form request and update the csv file with them?

While I was typing, the question was closing.

The cause of the error is that going to the URL in your browser sends a GET request, and it looks like your code is not handling this case (hence the 500 error). If you decide to support only POST requests for this endpoint, you can always check it with curl .

A slightly better practice (when using python) may be to use the flask-sqlalchemy plugin and create a sqlite database to store data instead of a csv file. However, it will be a little more. You can read about it here: http://flask-sqlalchemy.pocoo.org/2.3/

-one
source share

All Articles