在金字塔中没有一个公共api来实现这一点。然而,它有一个私有且相当稳定的API。。你已经被警告了。
路由匹配不仅仅发生在模式上,还发生在谓词上,因此它需要一个完整的请求对象来进行匹配,而不仅仅是一个url。
registry = config.registry or request.registry
mapper = registry.getUtility(pyramid.interfaces.IRoutesMapper)
result = mapper(request)
route, matchdict = result['route'], result['match']
if route is not None:
route # pyramid.interfaces.IRoute object
matchdict # request.matchdict
原始的
IRoute
对象本身
是
可通过introspector在公共api上获得。您可以在每个路由的基础上查找并匹配它们,但这将忽略金字塔固有的路由顺序。
introspector = config.registry or request.registry.introspector
intr = introspector.get('routes', route_name)
route = intr['route']
match = route.match(path)
if match is not None:
route # pyramid.interfaces.IRoute object
match # request.matchdict
路由上还有谓词,您可以传递
request
以确定谓词是否通过。