码迷,mamicode.com
首页 > 移动开发 > 详细

flask 中xx.init_app(app)方法

时间:2018-01-13 21:02:33      阅读:14      评论:0      收藏:0      [点我收藏+]

标签:工厂   UI   cto   not   port   ext   actor   follow   mail   

 

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) 

 

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)

flask 中xx.init_app(app)方法

标签:工厂   UI   cto   not   port   ext   actor   follow   mail   

原文地址:https://www.cnblogs.com/cuzz/p/8280183.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!