1. 程式人生 > >Flask--藍圖

Flask--藍圖

1.目錄結構

 

2.定義藍圖

給兩個應用分別定義一下藍圖

app/home/__init__.py:
    from flask import Blueprint
    home = Blueprint("home",__name__)
    import app.home.views

app/admin/__init__.py:
    from flask import Blueprint
    admin = Blueprint("admin",__name__)
    import app.admin.views

3.註冊藍圖

 
app/_init_.py:
    from flask import Flask
  #例項化Flask物件
    app = Flask(__name__)
  #開啟debug模式 app.debug = True from app.home import home as home_blueprint from app.admin import admin as admin_blueprint app.register_blueprint(home_blueprint)
  #url_prefix作用,訪問admin路由時,需要加admin字首 app.register_blueprint(admin_blueprint,url_prefix="/admin")

4.呼叫藍圖

app/home/views.py:
    from . import home
    @home.route("/")
    def index():
        return "前臺應用首頁"

app/admin/views.py:
from . import admin

    @admin.route("/")
    def index():
        return "後臺應用首頁"
#訪問admin應用時,需要加上admin字首

5.入口檔案

app同級目錄下建立manage.py

from app import app

if __name__ == "__main__":
    app.run(debug = True)