Pyramid Slash Routes

Say I have a route '/ foo / bar / baz'. I would also like to have another view matching "/ foo" or "/ foo /". But I do not want to systematically add trailing slashes for other routes, only for / foo and several others (/ buz, but not / biz)

From what I saw, I can’t just define two routes with the same route_name. I am currently doing this:

config.add_route('foo', '/foo') config.add_route('foo_slash', '/foo/') config.add_view(lambda _,__: HTTPFound('/foo'), route_name='foo_slash') 

Is there something more elegant in Pyramid for this?

+6
source share
3 answers

Found this solution when I was looking for the same thing for my project

 def add_auto_route(config,name, pattern, **kw): config.add_route(name, pattern, **kw) if not pattern.endswith('/'): config.add_route(name + '_auto', pattern + '/') def redirector(request): return HTTPMovedPermanently(request.route_url(name)) config.add_view(redirector, route_name=name + '_auto') 

And then during route setup

 add_auto_route(config,'events','/events') 

Instead of doing config.add_route('events','/events')

This is basically a hybrid of your methods. A new route has been defined with a name ending in _auto , and its representation is redirected to the original route.

EDIT

The solution does not take into account dynamic URL components and GET parameters. For a URL such as /abc/{def}?m=aasa , using add_auto_route() will cause a key error because the redirector function does not take request.matchdict into account. Below is the code. It also uses _query=request.GET to access the GET parameters.

 def add_auto_route(config,name, pattern, **kw): config.add_route(name, pattern, **kw) if not pattern.endswith('/'): config.add_route(name + '_auto', pattern + '/') def redirector(request): return HTTPMovedPermanently(request.route_url(name,_query=request.GET,**request.matchdict)) config.add_view(redirector, route_name=name + '_auto') 
+3
source

The pyramid has a way for HTTPNotFound views to automatically add a slash and check routes again for consistency (how Django works APPEND_SLASH=True ). Take a look at:

http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/urldispatch.html#redirecting-to-slash-appended-routes

According to this example, you can use config.add_notfound_view(notfound, append_slash=True) , where notfound is the function that defines your HTTPNotFound . If the view is not found (because it did not match due to the lack of a slash), the HTTPNotFound will add a slash and try again. The example shown in the link above is quite informative, but let me know if you have additional questions.

Also heed the warning that this should not be used with POST requests.

There are also many ways to throw a cat in Pyramid, so you can play and achieve it in different ways, but now you have a concept.

+10
source

I found another solution. It looks like we can link the two @view_config. Thus, this solution is possible:

 @view_config(route_name='foo_slash', renderer='myproject:templates/foo.mako') @view_config(route_name='foo', renderer='myproject:templates/foo.mako') def foo(request): #do something 

His behavior is also different from the question. The solution from the question is redirecting, so the URL changes in the browser. In the second form, both / foo and / foo / can be displayed in the browser, depending on the user input. I don't mind, but repeating the visualization path is also inconvenient.

+1
source

All Articles