2013-02-18 29 views
1

我想查看我的應用程序所有的路線。返回他們作爲響應像鍵=>值對︰金字塔:如何獲得視圖內的所有應用程序的路線?

'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) 

有一個RoutesConfiguratorMixinget_routes_mapper方法。我試圖導入類,並呼籲它的方法,但得到了一個錯誤,沒有registry是在它的實例:

from pyramid.config.routes import RoutesConfiguratorMixin as Router 

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

不工作。

回答

7

有兩種方式,一種支持(公共)和一種不支持(私人)。

選項#1將使用introspector並解釋爲here

選項#2將使用路由映射器(它不是公共API),方式是金字塔debugtoolbar在其routes panel中執行的操作。

+0

選項#1的文檔現在可以在這裏找到:http://docs.pylonsproject.org/projects/pyramid/en/1.6-branch/narr/introspector.html – russdot 2016-03-23 13:32:36

+0

哪個文檔是正確的取決於版本您正在使用的金字塔,可以在頁面切換(目前位於右下角),最新版本的鏈接目前位於:http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/ introspector.html – karfau 2016-06-16 12:58:21

相關問題