Flask框架 之使用蓝图实现模块划分

 

一、示例代码

run.py

from app import create_app

if __name__ == '__main__':
    app = create_app()
    app.run(debug=True, port=5000)

__init__.py

from flask import Flask
from app.controllers import config_blueprint

def create_app():
    app = Flask(__name__)
    # 注册蓝图
    config_blueprint(app)
    return app

controllers / __init__.py

from .IndexController import home
from .AdminController import admin

# 蓝本默认配置
default_blueprint = (
    # (蓝本,前缀)
    (home, ''),
    (admin, '/admin')
)

# 封装函数配置蓝本
def config_blueprint(app):
    for blueprint, url_prefix  in default_blueprint:
        app.register_blueprint(blueprint, url_prefix=url_prefix)

controllers / AdminController.py

from flask import Blueprint, render_template

admin = Blueprint("admin", __name__, url_prefix="", template_folder="templates")

@admin.route('/index')
@admin.route('/', methods = ['GET'])
def index():
    
    return render_template('admin/index.html')

 

posted @ 2023-03-31 10:48  样子2018  阅读(41)  评论(0编辑  收藏  举报