我试图了解如何最好地使用Flask重定向和传递参数
下面是我的代码,我发现x并且y没有将其纳入模板.
我的语法是否正确?我错过了什么基本的东西?我能够渲染模板,但我想重定向到网址/found,而不是只返回模板find.html
@app.route('/found')
def found(email,listOfObjects):
return render_template("found.html",
keys=email,obj=listOfObjects)
@app.route('/find', methods=['GET','POST'])
def find():
if request.method == 'POST':
x = 3
y = 4
return redirect(url_for('found',keys=x,obj=y))
return render_template("find.html")
Run Code Online (Sandbox Code Playgroud)
Ada*_*tek 48
重定向很好,问题在于found路线.您有多种方法可以将值传递给端点:作为路径的一部分,URL参数(用于GET请求)或请求体(用于POST请求).
换句话说,您的代码应如下所示:
@app.route('/found/<email>/<listOfObjects>')
def found(email, listOfObjects):
return render_template("found.html",
keys=email, obj=listOfObjects)
Run Code Online (Sandbox Code Playgroud)
或者:
@app.route('/found')
def found():
return render_template("found.html",
keys=request.args.get('email'), obj=request.args.get('listOfObjects'))
Run Code Online (Sandbox Code Playgroud)
此外,您的重定向应提供请求参数,而不是模板参数:
return redirect(url_for('found', email=x, listOfObjects=y))
Run Code Online (Sandbox Code Playgroud)
希望有所帮助.
| 归档时间: |
|
| 查看次数: |
54123 次 |
| 最近记录: |