我使用django建立一个博客网站。我使用python urllib.quote函数来编码url字符串而不提交表单。例如,在我的html文件中,有像这样的href链接:
http://example.com/post/?tag=new%20tag (1)
(1)是我从(2)编码的链接:
http://example.com/post/?tag=new tag (2)
这与形式GET方法略有不同,其中" new tag"被编码为" new + tag"。只要输出url不包含空格,两者都可以。
然而,在我点击网页中的链接(1)后,浏览器中生成的网址看起来像链接(2)(带空格)。我应该将链接保留为(2)吗?如何使输出网址为(1)或可能是以下标准网址:
http://example.com/post/?tag=new+tag (3)
答案 0 :(得分:2)
您可以使用urlencode
:
>>> import urllib
>>> urllib.urlencode({'tag': 'new tag'})
'tag=new+tag'
答案 1 :(得分:0)
您应该使用urllib.quote_plus
来完成编码。
https://docs.python.org/2/library/urllib.html#urllib.quote_plus