Django url // double slash removal (maybe Apache error)?

I have a url with this pattern:

('^(?i)somewhere/(\d+)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/$', somewherePage), 

where it should get the url for example:

 http://foo.com/somewhere/1337/foo/params/that/are//maybe///used/ 

Please note that some of the parameters are missing and lead to //. This stopped working, and instead of the correct url, I get something like:

 http://foo.com/somewhere/1337/foo/params/that/are/maybe/used/ 

where the required slashes are missing and my pattern is then not recognized. I think this may be caused by what my apache web server does, but I don't understand how to figure it out.

My question is how can I stop the removal of extra slashes or is there another solution for the dynamic number of parameters. Also this code worked fine, but its meaning was stopped. I'm not sure what has changed since the code base is outdated, but I know that this way of passing a variable number of parameters worked.

+4
source share
4 answers

there is another solution for the dynamic number of parameters

Yes, it is called a query string. Instead of this ugly template, use a simpler one (one that matches the required parameters), and pass everything else after ? , eg. example.com/foo/42?p1=foo&p2=bar . Then you can extract them from request.GET .

+4
source

I came across this question after debugging a similar problem in Django. Extra slash always decreased due to nginx. Adding the merge_slashes off configuration to nginx merge_slashes off problem.

+3
source

If you want to avoid the ugliness of GET parameters, and since this view somewherePage implicitly knows that it expects a variable number of parts back from the reference URL, you can transfer the parse code of the URL into the view itself.

That is, create a url template as:

 ('^(?i)somewhere/(\d+)/(.*)/$', somewherePage), 

and split it at the beginning of your view:

 def somewherePage(request, somewhere_int, somewhere_pieces): import string the_pieces = string.split(somewhere_pieces, '/') 
+1
source

The answer to what caused the extra // to be deleted are some of the mod_rewrite Apache module commands. I decided to simply fix the way I create URLs, as PiotrLegnica suggests.

0
source

Source: https://habr.com/ru/post/1339424/


All Articles