之前我们写的url和视图函数都是处在同一个文件,如果项目比较大的话,这显然不是一个合理的结构,而蓝图可以优雅的帮我们实现这种需求。以下看一个使用蓝图的文件的例子:
本节基于 Python Flask Web教程016:模板继承
目录结构 apps.about.pyfrom flask import Blueprint, render_template
bp = Blueprint('about', __name__, url_prefix='/about')
@bp.route('/')
def about():
return render_template('about.html')
apps.control.py
from flask import Blueprint, render_template
bp = Blueprint('control', __name__, url_prefix='/control')
@bp.route('/')
def control():
return render_template('control.html')
app.py
from flask import Flask, render_template, url_for
from apps.control import bp as control_bp
from apps.about import bp as about_bp
app = Flask(__name__)
app.register_blueprint(control_bp)
app.register_blueprint(about_bp)
@app.route('/')
def hello_world():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)



