【发布时间】:2016-06-08 01:29:47
【问题描述】:
在 Flask 中,如何在渲染模板时重定向到不同的页面?
@app.route('/foo.html', methods=['GET', 'POST'])
def foo():
if request.method == "POST":
x = request.form['x']
y = request.form['y']
if x > y:
return redirect(render_template("bar.html", title="Bar"))
else:
return render_template("foo.html", title="Foo")
return render_template("foo.html", title="Foo")
@app.route('/bar.html')
def bar():
return render_template("bar.html", title="Bar")
return redirect(render_template("bar.html", title="Bar")) 导致将被模板化和显示的整个页面改为显示在 URL 中:
http://localhost:5000/<!DOCTYPE html><html><head><title>Bar</title></head>...
这会导致 404 错误,因为此页面不存在。
我已经尝试过redirect(url_for('bar')),但我需要将 Jinja2 变量传递给 url_for 并将其作为页面模板。
【问题讨论】: