本文介绍一下蓝图的简单应用案例
第一步:在视图中定义路由(facebook\views\profile.py)
from flask import Blueprint, render_template profile = Blueprint('profile', __name__) # 就像普通的@app.route一样定义 @profile.route('/<name>') def hello(name): return 'hello %s' % name*也可以在定义蓝图的时候指定静态文件和模板文件的目录
profile = Blueprint('profile', __name__, template_folder='templates', static_folder='static')第二步: Flask 应用程序中注册它(facebook\__init__.py)
from flask import Flask from .views.profile import profile app = Flask(__name__) # 为应用程序注册蓝图 app.register_blueprint(profile)第二步: 启动Flask 服务(facebook\runserver.py)
from facebook import app app.run()源码地址:
https://github.com/xcaojianhong/git/tree/master/facebook