Python 从蓝图中获取restful路线
有没有办法在蓝图中定义路线?我知道这个()snippit存在,但需要初始化应用程序才能使用url\u映射。有没有一种不用应用程序就能查看路线的方法?我正在使用flask restful开发一个api,我想在不使用应用程序的情况下显示蓝图中的路线以保持其独立性。使用polyfunc提供的信息,我能够想出以下解决方案:Python 从蓝图中获取restful路线,python,flask,flask-restful,Python,Flask,Flask Restful,有没有办法在蓝图中定义路线?我知道这个()snippit存在,但需要初始化应用程序才能使用url\u映射。有没有一种不用应用程序就能查看路线的方法?我正在使用flask restful开发一个api,我想在不使用应用程序的情况下显示蓝图中的路线以保持其独立性。使用polyfunc提供的信息,我能够想出以下解决方案: from flask import Blueprint, current_app, url_for from flask_restful import Resource, Api
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)]
要访问URL,应用程序必须运行。要从蓝图中访问它,您可以从flask import current_app执行
,并使用您引用的代码段,方法是将app
替换为current_app
。也许这个答案也有帮助: