【问题标题】:Pyramid: How to get all app's routes within a view?Pyramid:如何在视图中获取所有应用程序的路线?
【发布时间】:2021-02-10 08:12:12
【问题描述】:

我想查看我的应用程序拥有的所有路由。将它们作为响应返回,例如 key=>value 对:

'route1' => '{foo:\w+}'
'route2' => '{baz:\w+\d+}'
... and so on

但我不知道如何让它们进入我的视野。例如,这是我的观点。我希望它返回路线图。我这样做:

@view_config(route_name='route1')
def someView(request):
    routes = request.registry.settings.getRoutes()  ## what should I print here to get a map of routes?
    r = ''
    for k,v in sorted(routes.items()):
        r += str(k) + "=>" + str(v) + "<br/>";
    return Response(r)

有一个带有get_routes_mapper 方法的RoutesConfiguratorMixin 类。我尝试导入该类并调用它的方法,但得到一个错误,即它的实例中没有registry

from pyramid.config.routes import RoutesConfiguratorMixin as Router

r = Router();
routes = r.get_routes_mapper();
## ... and the same code as above

没用。

【问题讨论】:

    标签: python python-3.x pyramid


    【解决方案1】:

    有两种方式,一种支持(公开),一种不支持(私有)。

    选项 #1 是使用自省器,并解释为 here

    选项 #2 是使用路由映射器(不是公共 api),就像金字塔调试工具栏在其 routes panel 中所做的那样。

    【讨论】:

    【解决方案2】:

    Pyramid 为此安装了一个名为 proutes 的 bin 脚本。

    【讨论】:

    • 这不是一个答案,而是一个足够的评论,请考虑下次使用评论部分
    【解决方案3】:

    然后安装 pshell pshell 使用您的应用程序配置登录到 pshell。 然后运行

    print("\n".join([r.path for r in app.routes_mapper.routelist]))
    

    【讨论】:

      猜你喜欢
      • 2011-04-10
      • 2013-03-08
      • 2021-07-31
      • 2012-11-21
      • 2012-09-17
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      相关资源
      最近更新 更多