【问题标题】:How to display @ instead of being encoded as %40 (urlencode) in browser address bar?如何在浏览器地址栏中显示 @ 而不是被编码为 %40 (urlencode)?
【发布时间】:2016-07-12 03:37:13
【问题描述】:

我尝试在 Python3 (Flask) 项目中使用这一行,它是 UTF-8 编码:

@app.route('/@<username>', methods=['GET', 'POST'])

但在浏览器地址栏中,它总是显示为:

http://example.com/%40username

如何在浏览器地址栏中显示原来的'@'?谢谢!

【问题讨论】:

    标签: python html url flask


    【解决方案1】:

    你是在 python 代码中使用 url_for 吗?在这种情况下,一种方法是在 url_for 的返回值上调用 urllib.unquote

    示例代码:

    from flask import Flask, url_for, redirect
    import urllib
    app = Flask(__name__)
    
    @app.route("/")
    def hello():
        return redirect(urllib.unquote(url_for('user', username='test')))
    
    @app.route('/@<username>', methods=['GET', 'POST'])
    def user(username):
        return "Hello %s" % (username)
    
    if __name__ == "__main__":
        app.run()
    

    讨论禁用字符转义here

    【讨论】:

    • PS,当我在浏览器地址栏中输入“example.com/@username”时,@ 显示为正常@。我已经尝试过您的示例代码,它也适用于手动输入。我的问题是当这条路线从模板文件呈现时,例如。 href="{{ url_for('.user', username=username) }},它在新页面的地址栏上显示@为%40。
    • 这个链接有用吗? stackoverflow.com/questions/27300332/…
    • 谢谢凯文,你推荐的链接给了我帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 2013-07-12
    • 2021-11-02
    相关资源
    最近更新 更多