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
dddd
V2EX  ›  Python

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

  •  
  •   dddd · 2014-10-20 17:02:36 +08:00 · 12255 次点击
    这是一个创建于 3474 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比如说用户登录成功了,将其重定向到另一个页面(可以发表文章)。

    用的 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 条回复    2014-10-23 07:19:44 +08:00
    ipconfiger
        1
    ipconfiger  
       2014-10-20 17:44:10 +08:00
    Flask的话直接redirect就可以了
    dddd
        2
    dddd  
    OP
       2014-10-20 19:03:41 +08:00
    @ipconfiger
    不用第三方框架,除了模板引擎
    shajiquan
        3
    shajiquan  
       2014-10-21 15:46:05 +08:00
    加个 HTTP 状态码 302 ?然后浏览器应该会处理你的跳转。
    dddd
        4
    dddd  
    OP
       2014-10-21 15:56:25 +08:00
    @shajiquan
    301,302,303都试了。。。
    我得看看我的代码逻辑是不是出问题了
    MasterYoda
        5
    MasterYoda  
       2014-10-21 16:11:23 +08:00   ❤️ 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  
       2014-10-21 16:52:09 +08:00   ❤️ 1
    @dddd 加油。别忘了 headers 里的 Location。
    dddd
        7
    dddd  
    OP
       2014-10-22 09:29:36 +08:00
    @shajiquan
    正解

    @MasterYoda
    再加上你的防止重定向不成功
    shajiquan
        8
    shajiquan  
       2014-10-22 11:34:09 +08:00
    @dddd Cool!
    ryd994
        9
    ryd994  
       2014-10-23 07:19:44 +08:00 via Android
    重定向的method不变吧……
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1615 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:58 · PVG 00:58 · LAX 09:58 · JFK 12:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.