简体   繁体   English

如何在python,django中将url解码为路径

[英]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 转换为路径,我得到的是这个 url,如下所示:

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.谢谢。

Use urllib.unquote to decode % -encoded string: 使用urllib.unquote解码% -encoded字符串:

>>> 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.quoteurllib.quote_plus ,你可以回来:

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

如果你使用Python3,你可以写

urllib.parse.unquote(url)

with python 3.9 and django 3.2 :使用python 3.9django 3.2

Solution 1 :解决方案 1:

import urllib
urllib.parse.unquote(url)

Solution 2 :解决方案 2:

from django.utils.encoding import uri_to_iri
uri_to_iri(url)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM