19 flask之蓝图
1 蓝图:对程序进行目录结构划分
2 蓝图相关
- Blueprint
- app.register_blueprint()
3 使用蓝图的步骤
- 生成蓝图对象
user = Blueprint('user', __name__, template_folder='template',static_folder='static')
2.使用
@user.route('/test')
def login():
return 'user test'
3.注册蓝图
app.register_blueprint(user.user, url_prefix='/user')
注意:url_prefix是访问路由的前缀,在生成蓝图对象或者注册蓝图时添加都行
4 蓝图的使用
目录结构:
-blueprint
-blue_test
-__init__.py
-static
-templates
-views
-order.py
-user.py
-manage.py
__init__py
from flask import Flask
app = Flask(__name__)
from blueprint.views import user
from blueprint.views import order
app.register_blueprint(user.user)
app.register_blueprint(order.order)
manage.py
from blueprint import app
if __name__ == '__main__':
app.run()
user.py
from flask import Blueprint
user = Blueprint('user', __name__, url_prefix='/user')
@user.route('/test')
def login():
return 'user test'
order.py
from flask import Blueprint
order = Blueprint('order', __name__, url_prefix='/order')
@order.route('/test')
def test():
return 'order test'

总结:
1 xxx = Blueprint('account', name,url_prefix='/xxx') :蓝图URL前缀,表示url的前缀,在该蓝图下所有url都加前缀
2 xxx = Blueprint('account', name,url_prefix='/xxx',template_folder='tpls'):给当前蓝图单独使用templates,向上查找,当前找不到,会找总templates
3 蓝图的befort_request,对当前蓝图有效
4 大型项目,可以模拟出类似于django中app的概念