【问题标题】:Encode URL path and show it in browser编码 URL 路径并在浏览器中显示
【发布时间】:2015-04-15 18:12:49
【问题描述】:

我使用 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) 后,浏览器中生成的 url 看起来像链接 (2)(带空格)。我应该将链接保留为 (2) 吗?如何使输出 url 为 (1) 或以下标准:

 http://example.com/post/?tag=new+tag  (3)

【问题讨论】:

    标签: html django url get encode


    【解决方案1】:

    你可以使用urlencode:

     >>> import urllib
     >>> urllib.urlencode({'tag': 'new tag'})
    'tag=new+tag'
    

    【讨论】:

    • 这正是我想要的。
    【解决方案2】:

    您应该改用urllib.quote_plus 来往返编码。

    https://docs.python.org/2/library/urllib.html#urllib.quote_plus

    【讨论】:

      猜你喜欢
      • 2021-01-25
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      • 2014-06-18
      • 2018-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多