从蓝图中获取 flask-restful 路由
Getting flask-restful routes from within a blueprint
有没有办法获取蓝图中定义的路由?我知道这个 (http://flask.pocoo.org/snippets/117/) 片段存在,但需要初始化应用程序才能使用 url_map。有没有办法在没有应用程序的情况下查看路线?我正在用 flask-restful 开发一个 api,我想在没有应用程序的情况下显示蓝图中的路线,以保持其独立性。
利用 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)]
有没有办法获取蓝图中定义的路由?我知道这个 (http://flask.pocoo.org/snippets/117/) 片段存在,但需要初始化应用程序才能使用 url_map。有没有办法在没有应用程序的情况下查看路线?我正在用 flask-restful 开发一个 api,我想在没有应用程序的情况下显示蓝图中的路线,以保持其独立性。
利用 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)]