Flask视图引发TypeError:'bool'对象不可调用

时间:2014-01-28 12:53:02

标签: python flask flask-login

我正在尝试在我的Flask应用中调试一个视图,该视图在回溯中返回错误TypeError: 'bool' object is not callable的500状态。视图从Flask-Login调用login_user然后返回True以指示登录成功。

我已调试到app_iter = app(environ, start_response),而app现在是一个值为True的布尔值,而不是Flask app对象。

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

1 个答案:

答案 0 :(得分:27)

在Flask中,视图必须返回以下内容之一:

  • 一个字符串
  • 一个Response对象(或子类)
  • (string, status, headers)(string, status)
  • 的元组
  • 有效的WSGI应用程序

Flask测试前3个选项,如果它们不适合,则认为它是第四个。你在某个地方返回了True,而它被视为一个WSGI应用程序。

请参阅文档中的About Responses