python中redirect如何传值_传递参数时重定向 - python

在烧瓶中,我可以这样做:

render_template("foo.html", messages={'main':'hello'})

如果foo.html包含{ { messages['main'] }},则页面将显示hello。但是,如果有一条通往foo的路线怎么办:

@app.route("/foo")

def do_foo():

# do some logic here

return render_template("foo.html")

在这种情况下,如果我仍然希望这种逻辑发生,那么进入foo.html的唯一方法是通过redirect:

@app.route("/baz")

def do_baz():

if some_condition:

return render_template("baz.html")

else:

return redirect("/foo", messages={"main":"Condition failed on page baz"})

# above produces TypeError: redirect() got an unexpected keyword argument 'messages'

因此,如何获取该messages变量以传递给foo路由,这样我不必在加载该路由之前就重写该路由所计算的相同逻辑代码?

参考方案

您可以将消息作为显式URL参数(正确编码)传递,或在重定向之前将消息存储到

你可能感兴趣的:(python中redirect如何传值_传递参数时重定向 - python)