蓝图作用
相当于一个功能模块,能做到即插即用
蓝图运用示例1:
简单项目示例
account.py
#!/usr/bin/env python # -*- coding:utf-8 -*- from flask import Blueprint from flask import render_template from flask import request account = Blueprint('acc', __name__) @account.route('/login.html', methods=['GET', "POST"]) def login(): return render_template('login.html')
blog.py
#!/usr/bin/env python # -*- coding:utf-8 -*- from flask import Blueprint,url_for blog = Blueprint('blog', __name__) @blog.before_request def a(): print("wo我是blog 的请求扩展") @blog.route("/index") def index(): print(url_for("acc.login")) return "ok"
user.py
#!/usr/bin/env python # -*- coding:utf-8 -*- from flask import Blueprint user = Blueprint('user', __name__)
__init__.py
#!/usr/bin/env python # -*- coding:utf-8 -*- from flask import Flask app = Flask(__name__,template_folder='templates',static_folder='statics',static_url_path='/static') @app.before_request def a(): print("我是app里面的befor_request") from .views.account import account from .views.blog import blog from .views.user import user app.register_blueprint(account) app.register_blueprint(blog) app.register_blueprint(user)
run.py
from pro_flask import app if __name__ == '__main__': app.run()
复杂项目:
admin/__init__.py
from flask import Blueprint admin = Blueprint( 'admin', __name__, template_folder='templates', static_folder='static' ) from . import views
admin/views.py
from . import admin @admin.route('/index') def index(): return 'Admin.Index'
web/__init__.py
from flask import Blueprint web = Blueprint( 'web', __name__, template_folder='templates', static_folder='static' ) from . import views
web/views.py
from . import web @web.route('/index') def index(): return 'Web.Index'
__init__.py
#!/usr/bin/env python # -*- coding:utf-8 -*- from flask import Flask from .admin import admin from .web import web app = Flask(__name__) app.debug = True app.register_blueprint(admin, url_prefix='/admin') app.register_blueprint(web)
run.py
from pro_flask import app if __name__ == '__main__': app.run()