Reputation: 7176
I am trying to figure out if its possible to get a route name in Pyramid by the URL (not the Request) URL but a string URL. So lets say I have a request and the path is /admin/users/manage. I know you can match the route name to get the route_name of the request but how can I get the route name of /admin and the route name of /admin/users?
introspector.get('routes', 'admin')
works to get the route path of the admin route but is it possible to work the other way around?
introspector.get('routes', 'admin/users')
Basically is there a way to get the route_objects of all routes under the admin/ prefix? Introspector looked like I could loop thru ALL routes but not query all the specific routes within within a path.
Upvotes: 3
Views: 929
Reputation: 6059
I had to dig into the pyramid source code to get this solution.
Also, just to clarify: This solution will provide you with the correct route_name
for the supplied route_url
.
If I have my_route
as the route name and /foo/bar
as the url, I set the following my_url
variable to "/foo/bar" and it returns "my_route"
from pyramid.interfaces import IRoutesMapper
@view_config(...)
def call_route_by_url(request):
routes = request.registry.queryUtility(IRoutesMapper).get_routes()
my_url = '/your/url/here'
for r in routes:
if r.path == my_url:
# do something with r.name <-- that is your route_name
pass
Upvotes: 2