Flask视图引发TypeError:'bool'对象不可调用 [英] Flask view raises TypeError: 'bool' object is not callable

查看:402
本文介绍了Flask视图引发TypeError:'bool'对象不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的Flask应用程序中调试一个视图,该视图返回500状态,并在回溯中出现错误 TypeError:'bool'object is callable 。该视图从Flask-Login调用 login_user ,然后返回 True 来表示登录成功。 b
$ b

我调试过直到 app_iter = app(environ,start_response) app 现在是一个布尔值,其值 True 而不是Flask应用程序对象。

  Traceback(最近一次调用最后一次):
在__call__
中返回第1836行的D:\Python27\lib\site-packages\flask\app.py文件self.wsgi_app(environ,start_response)
在wsgi_app
response = self中的文件D:\Python27\lib\site-packages\flask\app.py,行1820。 make_response(self.handle_exception(e))
文件D:\Python27\lib\site-packages\flask\app.py,行1403,在handle_exception
reraise(exc_type ,exc_value,tb)
文件D:\Python27\lib\site-packages\flask\app.py,行1817,在wsgi_app
res ponse = self.full_dispatch_request()
文件D:\Python27\lib\site-packages\flask\app.py,第1478行,在full_dispatch_request
response = self.make_response (rv)
文件D:\Python27\lib\site-packages\flask\app.py,行1577,在make_response
rv = self.response_class.force_type(rv ,request.environ)
文件D:\Python27\lib\site-packages\werkzeug\wrappers.py,行824,in force_type
response = BaseResponse(* _ run_wsgi_app响应,环境))
文件D:\Python27\lib\site-packages\werkzeug\wrappers.py,第57行,在_run_wsgi_app
返回_run_wsgi_app(* args)
文件D:\Python27\lib\site-packages\werkzeug\test.py,第854行,在run_wsgi_app
app_iter = app(environ,start_response)
TypeError :'bool'对象不可调用





  @ app.route('/登录',methods = ['POST'])
def login():
username = request.form ['username']
user = User.query.filter_by(username = username)如果用户:
login_user(用户)
返回True


/ pre>

解决方案

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


  • 一个字符串
  • a 响应对象(或子类) $ b
  • 元组(字符串,状态,标题)(字符串,状态)

  • 有效的WSGI应用程序



前3个选项的Flask测试,如果不合适,假设是第四个。您在某处返回了True,并将其视为WSGI应用程序。



请参阅关于答复的文档。


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

I have debugged until app_iter = app(environ, start_response) and the app is now a boolean with the value True rather than the Flask app 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

解决方案

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视图引发TypeError:'bool'对象不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆