V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
fonlan
V2EX  ›  Python

Flask <path:url>的坑?

  •  
  •   fonlan · 2019-04-18 18:15:32 +08:00 · 2147 次点击
    这是一个创建于 1806 天前的主题,其中的信息可能已经有所发展或是发生改变。

    刚入 Flask 的坑,需要用到一个简单的 url 重定向,代码如下:

    @app.route('/redirect/<path:url>')
    def redirect_to(url):
        print(url)
        return redirect(url)
    

    需要重定向的一个 url 如 http://a.com/?b=1,但是代码里 print 出来的 url 却把问号之后的都给吃了,只剩 http://a.com/了。没问号的 url 重定向就没问题。 求大神解答下!

    4 条回复    2019-04-18 21:47:58 +08:00
    mayorbryant
        1
    mayorbryant  
       2019-04-18 18:58:26 +08:00   ❤️ 1
    ?后面是 get 请求的参数部分
    whusnoopy
        3
    whusnoopy  
       2019-04-18 19:10:43 +08:00   ❤️ 1
    参考 http://flask.pocoo.org/docs/1.0/api/#incoming-request-data
    你需要的是自己解析 request.full_path

    另外,在 Flask 的 route 里,path 相对于普通的 string 只是多支持了 slash (正斜杠),并没有支持 params,详见 http://flask.pocoo.org/docs/1.0/api/#url-route-registrations
    fonlan
        4
    fonlan  
    OP
       2019-04-18 21:47:58 +08:00 via Android
    感谢感谢,明白了😊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2943 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 56ms · UTC 15:16 · PVG 23:16 · LAX 08:16 · JFK 11:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.