赋料扬雄敌,诗看子建亲。这篇文章主要讲述flask 中xx.init_app(app)方法相关的知识,希望能为你提供帮助。
bootstrap = Bootstrap() mail = Mail() moment = Moment() db = SQLAlchemy() def create_app(config_name): app = Flask(__name__) #将指定的配置通过from_object()方法导入app.config字典配置对象 app.config.from_object(config[config_name]) config[config_name].init_app(app) bootstrap.init_app(app) mail.init_app(app) moment.init_app(app) db.init_app(app)
【flask 中xx.init_app(app)方法】http://flask.pocoo.org/docs/0.12/patterns/appfactories/
flask 文档关于工厂模式里面提到了上面的这种套路。
It ’ s preferable to create your extensions and app factories so that the extension object does not initially get bound to the application.
Using Flask-SQLAlchemy, as an example, you should not do something along those lines:
def create_app(config_filename):
app = Flask(__name__)
app.config.from_pyfile(config_filename)
db = SQLAlchemy(app)
But, rather, in model.py (or equivalent):
db = SQLAlchemy()
and in your application.py (or equivalent):
def create_app(config_filename):
app = Flask(__name__)
app.config.from_pyfile(config_filename)
from yourapplication.model import db
db.init_app(app)
推荐阅读
- 用java和junit编写app自动化测试用例
- bean工厂和ApplicationContext
- 乐视超级电视s40 air价格多少钱?乐视电视s40 air价格
- 乐视超级电视s50 air价格多少钱?乐视s50 air超级电视价格
- ipad air2设置怎样?ipad air2设置参数
- ipad air2尺寸多大?ipad air2屏幕尺寸大小
- 天猫魔筒是啥?天猫魔筒技巧设置
- 天猫魔筒申请成功后啥时候发货?天猫魔筒申请成功后发货时间
- 天猫魔筒价格多少钱?天猫魔筒安全版价格