【问题标题】:Drop response body when redirecting in flask在烧瓶中重定向时丢弃响应正文
【发布时间】:2018-05-24 05:22:36
【问题描述】:

我搞定了

@app.route('/hello')
def hello():
    return redirect('/world', code=307)

然而,响应 body 包含一些漂亮的 html。当我重定向到 image 时,这通常不是很有帮助。相反,我只想删除响应正文,或者更好地将其设置为有用的字符串或类似的东西。

【问题讨论】:

  • 尝试类似:from flask import Request; [...]; request.data=""

标签: python flask http-redirect


【解决方案1】:

HTML 重定向响应是硬编码在源代码中的:

werkzeug.utils.redirect

按照他们的模式,您可以为您的用例自定义重定向功能,如下所示:

from flask import current_app, url_for

@app.route('/hello')
def hello():
    response = current_app.response_class(
        response="Hello, world",
        status=307,
        mimetype="text/plain"
    )
    response.headers["Location"] = url_for('.world')
    return response

【讨论】:

    猜你喜欢
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 2014-04-29
    • 1970-01-01
    相关资源
    最近更新 更多