从蓝图中获取烧瓶安息路线

时间:2016-09-30 13:36:36

标签: python flask flask-restful

有没有办法在蓝图中定义路线?我知道这个(http://flask.pocoo.org/snippets/117/)snippit存在,但需要让应用程序初始化才能使用url_map。有没有办法在没有申请的情况下查看路线?我正在开发一个带有烧瓶的api,我想在蓝图中显示路线,而不是应用程序保持自包含。

1 个答案:

答案 0 :(得分:0)

使用polyfunc提供的信息,我能够提出这个解决方案:

from flask import Blueprint, current_app, url_for
from flask_restful import Resource, Api

api_blueprint = Blueprint('api', __name__)
api = Api(api_blueprint)

@api.resource('/foo')
class Foo(Resource):
    def get(self):
        prefix = api_blueprint.name + '.'  # registered classes will have this as their prefix

        # Get current rules
        rules = current_app.url_map.iter_rules()

        # Get list from routes registered under the blueprint
        routes = [url_for(rule.endpoint) for rule in rules if rule.endpoint.startswith(prefix)]