blueprint的作用
blueprint最主要的是解決路由的問題
建立blueprint的步驟
- 建立blueprint
- 註冊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