[英]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.