Flask中的藍圖 --Blueprint
阿新 • • 發佈:2020-12-11
專案目錄:
藍圖必須使用Blueprint例項化一個物件,Blueprint也是繼承的Flask
#app1.views.py檔案程式碼 #藍圖 from flask import Blueprint,render_template,send_file #Blueprint 當作一個不能run的Flask例項 user = Blueprint("app01user",__name__,url_prefix="/admin",template_folder="/flsakdemo1/app01/templates",static_folder="/flsakdemo1/app01/static",static_url_path="/app01/static") #第一個引數藍圖名 url_prefix @user.route("/loginOne") def loginOne(): return "I am app01user Blueprint loginOne func" @user.route("/sendFiles") def sendFiles(): return send_file("01.png")
#app2.views.py檔案程式碼 #藍圖 from flask import Blueprint #Blueprint 當作一個不能run的Flask例項 user = Blueprint("app02user",__name__,url_prefix="/shopping") #第一個引數藍圖名 url_prefix url字首:用於隔離相同url,藍圖名稱不能重複,保證在app中唯一。 # app = Flask(__name__) @user.route("/loginOne") def loginOne(): return "I am app02user Blueprint loginOne func"
使用藍圖
from flask import Flask app = Flask(__name__) #Flask中的藍圖 Blueprint #藍圖作用:功能隔離,路由隔離#建立藍圖註冊:結合app01,app02一起看 from flsakdemo1.app01.views import user app.register_blueprint(user) from flsakdemo1.app02.views import user as app02_user app.register_blueprint(app02_user) if __name__ == '__main__': app.run()