python庫--flask--建立嵌套藍圖

  • 這裏沒有對內容進行py文件分割, 能夠本身根據框架本身放入對應位置
  • 如下代碼生成一個 /v1/myapp/test 的路由
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)
相關文章
相關標籤/搜索