Python-Flask视图引发TypeError:“ bool”对象不可调用

我试图调试我的Flask应用程序中的视图,该视图返回500状态,并TypeError: 'bool' object is not callable在追溯中出现错误。然后login_userFlask-Login进行的视图调用返回True以指示登录成功。

我进行了调试,直到app_iter = app(environ, start_response),app现在是具有值True而不是Flask应用程序对象的布尔值。

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

回答:

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

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

前三个选项的Flask测试,如果不合适,则假定为第四个。你在某个地方返回了True,它将被视为WSGI应用程序。

请参阅文档中的关于响应。

以上是 Python-Flask视图引发TypeError:“ bool”对象不可调用 的全部内容, 来源链接: utcz.com/qa/426255.html

回到顶部