简体   繁体   English

安装 CORS 后 flask 服务器出错:AttributeError: 'FlaskApp' object 没有属性 'after_request'

[英]Error with flask server after installing CORS: AttributeError: 'FlaskApp' object has no attribute 'after_request'

I have the following code for an flask server:我有 flask 服务器的以下代码:

from flask import render_template
import connexion

# Create the application instance
app = connexion.App(__name__, specification_dir="./")

# read the swagger.yml file to configure the endpoints
app.add_api("swagger.yml")

# Create a URL route in our application for "/"
@app.route("/")
def home():
    """
    This function just responds to the browser URL
    localhost:5000/

    :return:        the rendered template "home.html"
    """
    return render_template("home.html")


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

Which works fine, but I need to add CORS support, so I intalled the library:哪个工作正常,但我需要添加 CORS 支持,所以我安装了库:

pip3 install -U flask-cors

And the lines:和行:

from flask_cors import CORS
CORS(app)

The rest remains the same: rest 保持不变:

from flask import render_template
from flask_cors import CORS
import connexion


# Create the application instance
app = connexion.App(__name__, specification_dir="./")

# read the swagger.yml file to configure the endpoints
app.add_api("swagger.yml")
CORS(app)

# Create a URL route in our application for "/"
@app.route("/")
def home():
    """
    This function just responds to the browser URL
    localhost:5000/

    :return:        the rendered template "home.html"
    """
    return render_template("home.html")


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

But now, when I try to run it I get, this error:但是现在,当我尝试运行它时,我得到了这个错误:

Traceback (most recent call last):
  File "server.py", line 15, in <module>
    CORS(app)
  File "/home/luis/.local/lib/python3.6/site-packages/flask_cors/extension.py", line 129, in __init__
    self.init_app(app, **kwargs)
  File "/home/luis/.local/lib/python3.6/site-packages/flask_cors/extension.py", line 154, in init_app
    app.after_request(cors_after_request)
AttributeError: 'FlaskApp' object has no attribute 'after_request'

It works below.它在下面工作。 I think it's solved...我觉得解决了...

https://github.com/zalando/connexion/issues/438 https://github.com/zalando/connexion/issues/438

from flask import render_template
from flask_cors import CORS
import connexion


# Create the application instance
app = connexion.App(__name__, specification_dir="./")

# read the swagger.yml file to configure the endpoints
app.add_api("swagger.yml")
CORS(app.app) # this

# Create a URL route in our application for "/"
@app.route("/")
def home():
    """
    This function just responds to the browser URL
    localhost:5000/

    :return:        the rendered template "home.html"
    """
    return render_template("home.html")


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sanic( ) - AttributeError: 'Request' object 没有属性 'split' - sanic( ) - AttributeError: 'Request' object has no attribute 'split' Flask + Ajax 集成:AttributeError:&#39;WSGIRequestHandler&#39; 对象没有属性 &#39;environ&#39; - Flask + Ajax Integration : AttributeError: 'WSGIRequestHandler' object has no attribute 'environ' 格式错误:AttributeError: 'JsonResponse' object has no attribute '_headers' - Error in formatting: AttributeError: 'JsonResponse' object has no attribute '_headers' 在JQuery中启用CORS后,CORS请求被阻止 - CORS request blocked after enabling CORS in JQuery AttributeError问题:&#39;WebDriver&#39;对象没有属性&#39;manage&#39; - Issue with AttributeError: 'WebDriver' object has no attribute 'manage' Flask 的 AJAX 请求 CORS 策略错误 - AJAX Request CORS Policy error with Flask 安装 npm cors 包未解决 CORS 预检请求错误 - CORS preflight request error not solved by installing npm cors package AttributeError:&#39;WebElement&#39;对象没有属性&#39;getElementById&#39; - AttributeError: 'WebElement' object has no attribute 'getElementById' AttributeError:&#39;str&#39;对象没有属性&#39;resolve&#39; - AttributeError: 'str' object has no attribute 'resolve' AttributeError:&#39;NoneType&#39;对象没有属性&#39;is_active&#39; - AttributeError: 'NoneType' object has no attribute 'is_active'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM