V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
dddd
V2EX  ›  Python

POST 请求重定向到其它页面时, 如何改变成 GET 请求?

  •  
  •   dddd · Oct 20, 2014 · 13148 views
    This topic created in 4207 days ago, the information mentioned may be changed or developed.
    比如说用户登录成功了,将其重定向到另一个页面(可以发表文章)。

    用的 Python WSGI,通过 REQUEST_METHOD 和 PATH_INFO 判断该如何进行 start_response 以及返回那些数据,问题是我的 GET 请求的页面重定向能成功,可是 POST 之后再重定向,就会一直在加载,无法重定向...

    如下:
    if request_method == 'GET':# POST 一样
    ____if page.path.match(path_info):
    ________...
    ________start_response(xxx,xxx)
    ________return [page.data_for_get()] # page.data_for_post()
    9 replies    2014-10-23 07:19:44 +08:00
    ipconfiger
        1
    ipconfiger  
       Oct 20, 2014
    Flask的话直接redirect就可以了
    dddd
        2
    dddd  
    OP
       Oct 20, 2014
    @ipconfiger
    不用第三方框架,除了模板引擎
    shajiquan
        3
    shajiquan  
       Oct 21, 2014
    加个 HTTP 状态码 302 ?然后浏览器应该会处理你的跳转。
    dddd
        4
    dddd  
    OP
       Oct 21, 2014
    @shajiquan
    301,302,303都试了。。。
    我得看看我的代码逻辑是不是出问题了
    MasterYoda
        5
    MasterYoda  
       Oct 21, 2014   ❤️ 1
    response = Response(
    '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n'
    '<title>Redirecting...</title>\n'
    '<h1>Redirecting...</h1>\n'
    '<p>You should be redirected automatically to target URL: '
    '<a href="%s">%s</a>. If not click the link.' %
    (escape(location), display_location), code, mimetype='text/html')
    response.headers['Location'] = location
    return response
    这里的response是werkzeug的wrapper里的。
    shajiquan
        6
    shajiquan  
       Oct 21, 2014   ❤️ 1
    @dddd 加油。别忘了 headers 里的 Location。
    dddd
        7
    dddd  
    OP
       Oct 22, 2014
    @shajiquan
    正解

    @MasterYoda
    再加上你的防止重定向不成功
    shajiquan
        8
    shajiquan  
       Oct 22, 2014
    @dddd Cool!
    ryd994
        9
    ryd994  
       Oct 23, 2014 via Android
    重定向的method不变吧……
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1062 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 18:48 · PVG 02:48 · LAX 11:48 · JFK 14:48
    ♥ Do have faith in what you're doing.