金字塔:如何在视图中获取所有应用程序的路线?

时间:2013-02-18 02:05:50

标签: python python-3.x pyramid

我想看看我的申请所有的路线。将它们作为响应返回,例如key =>值对:

'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

不起作用。

3 个答案:

答案 0 :(得分:8)

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

选项#1是使用内省寻回器,并解释为here

选项#2是使用路径映射器(不是公共API),就像金字塔debugtoolbar在routes panel中所做的那样。

答案 1 :(得分:1)

金字塔为此安装了一个名为proutes的bin脚本。

答案 2 :(得分:1)

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

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