How to decode url to path in python, django
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'
If you are using Python3 you can write
urllib.parse.unquote(url)