Routes with trailing slashes in Pyramid

2024/9/29 17:25:09

Let's say I have a route '/foo/bar/baz'. I would also like to have another view corresponding to '/foo' or '/foo/'. But I don't want to systematically append trailing slashes for other routes, only for /foo and a few others (/buz but not /biz)

From what I saw I cannot simply define two routes with the same route_name. I currently do 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 to do this ?

Answer

Pyramid has a way for HTTPNotFound views to automatically append a slash and test the routes again for a match (the way Django's APPEND_SLASH=True works). Take a look at:

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

As per this example, you can use config.add_notfound_view(notfound, append_slash=True), where notfound is a function that defines your HTTPNotFound view. If a view is not found (because it didn't match due to a missing slash), the HTTPNotFound view will append a slash and try again. The example shown in the link above is pretty informative, but let me know if you have any additional questions.

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

There are also many ways to skin a cat in Pyramid, so you can play around and achieve this in different ways too, but you have the concept now.

https://en.xdnf.cn/q/71181.html

Related Q&A

understanding item for item in list_a if ... PYTHON

Ive seen the following code many times, and I know its the solution to my problems, but Im really struggling to understand HOW it works. The code in particular is:item for item in list_a if item not in…

Django redirect to custom URL

From my Django app, how to I redirect a user to somescheme://someurl.com?To give you some context in case it helps, I have a working oauth2 server written in Python/Django and I need to allow users to…

Python embedding with threads -- avoiding deadlocks?

Is there any way to embed python, allow callbacks from python to C++, allowing the Pythhon code to spawn threads, and avoiding deadlocks?The problem is this:To call into Python, I need to hold the GIL…

RuntimeError: Event loop is closed when using pytest-asyncio to test FastAPI routes

I received the errorRuntimeError: Event loop is closedeach time I try to make more than one async call inside my test. I already tried to use all other suggestions from other Stack Overflow posts to re…

Adjust threshold cros_val_score sklearn

There is a way to set the threshold cross_val_score sklearn?Ive trained a model, then I adjust the threshold to 0.22. The model in the following below :# Try with Threshold pred_proba = LGBM_Model.pre…

Efficiently insert multiple elements in a list (or another data structure) keeping their order

I have a list of items that should be inserted in a list-like data structure one after the other, and I have the indexes at which each item should be inserted. For example: items = [itemX, itemY, itemZ…

matplotlib versions =3 does not include a find()

I am running a very simple Python script: from tftb.generators import amgauss, fmlinI get this error: C:\Users\Anaconda3\envs\tf_gpu\lib\site-packages\tftb-0.0.1-py3.6.egg\tftb\processing\affine.py in …

How to fix Field defines a relation with the model auth.User, which has been swapped out

I am trying to change my user model to a custom one. I do not mind dropping my database and just using a new one, but when I try it to run makemigrations i get this errorbookings.Session.session_client…

Generate and parse Python code from C# application

I need to generate Python code to be more specific IronPyton. I also need to be able to parse the code and to load it into AST. I just started looking at some tools. I played with "Oslo" and …

An efficient way to calculate the mean of each column or row of non-zero elements

I have a numpy array for ratings given by users on movies. The rating is between 1 and 5, while 0 means that a user does not rate on a movie. I want to calculate the average rating of each movie, and t…