5

我想查看我的应用程序拥有的所有路由。将它们作为响应返回,例如 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)

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

from pyramid.config.routes import RoutesConfiguratorMixin as Router

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

不工作。

4

3 回答 3

10

有两种方式,一种是支持的(公共的),一种是不支持的(私有的)。

选项 #1 是使用自省器,并在此处进行说明。

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

于 2013-02-18T04:02:20.707 回答
4

Pyramid 安装了一个proutes为此目的调用的 bin 脚本。

于 2018-10-10T18:25:44.043 回答
1

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

print("\n".join([r.path for r in app.routes_mapper.routelist]))
于 2021-02-10T00:12:12.543 回答