推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
rogwan
V2EX  ›  Python

怎么让浏览器地址栏正常显示 @字符,而不是 urlencode 后的 %40 这样?

  •  
  •   rogwan · Jul 12, 2016 · 10315 views
    This topic created in 3769 days ago, the information mentioned may be changed or developed.
    (文件环境: Python3, flask jinja2, 文件是 utf-8 编码)

    我写了一个路由:
    @app.route('/@<username>', methods=['GET', 'POST'])
    def user(username):
    pass ...

    手工在浏览器里输入:
    example.com/@username ,这样 @字符在新版的浏览器里都可以正常显示的。
    但是,如果是从模板 Template 中渲染的链接 href="{{ url_for('.user', username=username) }} , 点击链接在新页面的浏览器地址栏里,@字符就会被转码成 %40 。

    这个怎么能让 url 依旧保持原来的编码啊?(虽然 @转成%40 功能不影响,但是 url 不优雅嘛 )
    15 replies  •  2016-07-13 11:37:41 +08:00
    dxwwym
    dxwwym
    Jul 12, 2016 via iPhone
    1
    楼主是处女座?
    firstway
    firstway
    Jul 12, 2016 via Android
    2
    “浏览器地址栏里,@字符就会被转码成 %40 。 ”我觉得没有办法不这样,除非你自己写一个浏览器。
    FrankFang128
    FrankFang128
    Jul 12, 2016
    3
    Chrome 就可以啊
    楼主你这个观察能力
    honkew
    honkew
    Jul 12, 2016
    4
    没有发现 @变成%40
    yeyeye
    yeyeye
    Jul 13, 2016
    5
    @dxwwym
    @firstway
    @FrankFang128

    @honkew

    我感觉你们误解了 楼猪说了 新版浏览器手测有用 但是后端生成链接的时候 程序把它转码了 所以要解决程序生成的时候的问题
    tifan
    tifan
    Jul 13, 2016
    6
    按照 RFC 3986, @ 符号是 reserved character ,进行 encode 是标准预期的行为。

    https://tools.ietf.org/html/rfc3986#section-2.2
    rogwan
    rogwan
    OP
    Jul 13, 2016
    7
    @yeyeye 谢谢解释,你的理解完全正确。
    rogwan
    rogwan
    OP
    Jul 13, 2016
    8
    @firstway
    @tifan

    建议可以看下 medium.com ,在任何页面点击用户名,跳转的页面 url 里都是 https://medium.com/@username 这个原生格式,@字符并没有被转码。
    LPeJuN6lLsS9
    LPeJuN6lLsS9
    Jul 13, 2016
    9
    这问题是你问的啊: https://stackoverflow.com/questions/38319627/how-to-display-instead-of-being-encoded-as-40-urlencode-in-browser-address

    就 unquote 一个吧。不过回答你问题的人举的例子,在 href 里用未 encode 的 url 就算了,还在 redirect 里用,会不会出问题?
    LPeJuN6lLsS9
    LPeJuN6lLsS9
    Jul 13, 2016
    10
    @rogwan 你在地址栏手动输这类符号浏览器会自动 decode 。你在页面里点链接,如果 href 是未 encode 的,浏览器也会自动 decode

    这类的浏览器指 firefox
    rogwan
    rogwan
    OP
    Jul 13, 2016
    11
    @hantsuki 你太牛了! SO 上的问题都被你抄家了。。。
    rogwan
    rogwan
    OP
    Jul 13, 2016
    12
    @hantsuki medium.com 做的很好啊,不管什么浏览器,连 IE 都显示原生 href 字符,不会进行 url 转码( Urlencode )。
    shunia
    shunia
    Jul 13, 2016
    13
    medium 可能是 js 处理的哦。。。
    mulog
    mulog
    Jul 13, 2016
    14
    这是你渲染模板时就已经 urlencode 了吧,我记得 url_for 是会做 urlencode 的, 你再把 %40 replace 掉呗 (也很不优雅)。。
    我只是好奇一下,你觉得 /@username 比 /username 好在哪里?
    21grams
    21grams
    Jul 13, 2016
    15
    我看了 stackoverflow 上的问题才明白楼主问的是什么,楼主的英文表达强于中文。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1027 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 18:44 · PVG 02:44 · LAX 10:44 · JFK 13:44
    ♥ Do have faith in what you're doing.