我正在为我的简单 REST 服务使用 Flask-Restless 扩展。现在我想启用 CORS。
根据这篇文章http://flask-restless.readthedocs.org/en/latest/customizing.html我尝试使用以下代码来启用 CORS:
import flask
import flask.ext.sqlalchemy
import flask.ext.restless
from flask import Flask
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:password@localhost/db'
db = flask.ext.sqlalchemy.SQLAlchemy(app)
class Cost(db.Model):
__tablename__ = 'costs'
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.Unicode, unique = True)
amount = db.Column(db.Float)
date = db.Column(db.DateTime)
def add_cors_headers(response):
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Credentials'] = 'true'
return response
db.create_all()
manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db = db)
blueprint = manager.create_api(Cost)
blueprint.after_request(add_cors_headers)
app.run()
但是blueprint
object 没有属性after_request
。
我做错了什么?