当前位置: 首页 > 工具软件 > Blueprint > 使用案例 >

Flask # 蓝图(Blueprint)的嵌套

田骁
2023-12-01
from flask import Flask
app = Flask(__name__)


from flask import Blueprint
# 二级蓝图
myapp = Blueprint('v1/myapp', __name__)
@myapp.route("/test", methods=["GET"])
def test():
    pass


from flask import Blueprint
class NestableBlueprint(Blueprint):
    def register_blueprint(self, blueprint, **options):
        def deferred(state):
            url_prefix = (state.url_prefix or u"") + (options.get('url_prefix', blueprint.url_prefix) or u"")
            if 'url_prefix' in options:
                del options['url_prefix']
            state.app.register_blueprint(blueprint, url_prefix=url_prefix, **options)
        self.record(deferred)


# 一级蓝图
v1 = NestableBlueprint('v1', __name__, url_prefix='/v1')
v1.register_blueprint(myapp, url_prefix="/myapp")
app.register_blueprint(v1)
 类似资料: