阅读 896

flask学习笔记之blueprint

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

文章分类
后端
文章标签