如何在没有查询字符串的情况下使用args重定向没有查询字符串?

时间:2015-08-21 06:39:56

标签: python flask http-redirect

我想将用户重定向到相同路径,但使用不同的参数。

这是我的代码:

@app.route('/foo')
def foo(args1=None, args2=None):
    return render_template('foo.html', args1=args1, args2=args2)

@app.route('/bar')
def redirect_to_foo():
    return redirect(url_for('foo', args1='something'))

但是当我看到网址时,会显示/foo?arg1=something... 如何使用args重定向但没有查询字符串?
有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

在HTTP 1.1中,实际上有一个status code (307)表示应该使用相同的方法重复请求并发布数据。尝试使用HTTP状态代码307 Internal Redirect而不是302。对于烧瓶,有一个参数code

@app.route('/bar', methods=['POST'])
def redirect_to_foo():
    return redirect(url_for('foo', args1='something'), code=307)

来源:Redirecting to URL in Flask

作为背景,我还会看一下我对OP的评论。

提示:也许这不是我回答的权利。我不明白你为什么要用不同的参数重定向。发回"不同的论点"作为重定向头旁边的HTTP有效负载是AFAIK(GET /&#34除外;在查询字符串"除外)是不可能的。如果你真的必须这样做,那就考虑结构。