flask學習筆記之blueprint

weixin_34239169發表於2018-06-07

blueprint的作用

blueprint最主要的是解決路由的問題

建立blueprint的步驟

  1. 建立blueprint
  2. 註冊blueprint

最簡程式碼示例

建立blueprint

from flask import Blueprint
 
admin_bp = Blueprint('admin', __name__)
 
@admin_bp.route('/index')
def index(name):
    return '<h1>Hello, this is admin blueprint</h1>'
複製程式碼

註冊blueprint

from flask import Flask
from admin.admin_module import admin_bp
 
app = Flask(__name__)
app.register_blueprint(admin_bp, url_prefix='/admin')
 
if __name__ == '__main__':
    app.run(debug=True)
複製程式碼

執行後訪問路徑http://localhost:5000/admin/index

相關文章