Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decode url to path in python, django

Hi I need to convert url to path, what i got is this url as bellow:

url = u'/static/media/uploads/gallery/Marrakech%2C%20Morocco_be3Ij2N.jpg' 

and what to be looked something like this:

path = u'/static/media/uploads/gallery/Marrakech, Morocco_be3Ij2N.jpg' 

thx.

like image 899
k.rozycki Avatar asked Oct 21 '14 14:10

k.rozycki


People also ask

Is URL and path same in Django?

The path function is contained with the django. urls module within the Django project code base. path is used for routing URLs to the appropriate view functions within a Django application using the URL dispatcher.

What is path converter Django?

Registering custom path convertersA converter is a class that includes the following: A regex class attribute, as a string. A to_python(self, value) method, which handles converting the matched string into the type that should be passed to the view function.

How does path work in Django?

Django path traversal or directory traversal is a web security vulnerability that gives a remote attacker access to files and directories that are stored outside the specified folder to which the application grants access. The attacker can achieve this by manipulating the files with a “dot-dot-slash” (../) sequence.


1 Answers

Use urllib.unquote to decode %-encoded string:

>>> import urllib >>> url = u'/static/media/uploads/gallery/Marrakech%2C%20Morocco_be3Ij2N.jpg' >>> urllib.unquote(url) u'/static/media/uploads/gallery/Marrakech, Morocco_be3Ij2N.jpg' 

Using urllib.quote or urllib.quote_plus, you can get back:

>>> urllib.quote(u'/static/media/uploads/gallery/Marrakech, Morocco_be3Ij2N.jpg') '/static/media/uploads/gallery/Marrakech%2C%20Morocco_be3Ij2N.jpg' 
like image 74
falsetru Avatar answered Sep 29 '22 13:09

falsetru