Python 3.x 在重定向期间将变量传递到Python Flask中的另一个路由
我有一个名为upload_file_to_s3的函数,它将文件上传到我的s3存储桶。当我使用POST方法时,我希望将文件上载到s3,并将一个变量(即我上载的文件的文件名)传递到我的第一个路径“/home”。但是,我不能只是在“重定向”中传递变量,对吗 下面是我试图实现的目标的说明:Python 3.x 在重定向期间将变量传递到Python Flask中的另一个路由,python-3.x,flask,Python 3.x,Flask,我有一个名为upload_file_to_s3的函数,它将文件上传到我的s3存储桶。当我使用POST方法时,我希望将文件上载到s3,并将一个变量(即我上载的文件的文件名)传递到我的第一个路径“/home”。但是,我不能只是在“重定向”中传递变量,对吗 下面是我试图实现的目标的说明: @app.route("/home") def home(variable_filename): *some functions here using the <variable_filename>
@app.route("/home")
def home(variable_filename):
*some functions here using the <variable_filename>*
return render_template('home.html')
@app.route("/upload", methods=['POST'])
def upload():
if request.method == "POST":
f = request.files['file']
f.save(os.path.join(UPLOAD_FOLDER, f.filename))
upload_file_to_s3(f"uploads/{f.filename}", BUCKET)
variable_filename = f.filename
return redirect("/home",variable_filename)
@app.route(“/home”)
def home(变量_文件名):
*这里的一些函数使用*
返回渲染模板('home.html')
@app.route(“/upload”,方法=['POST']))
def upload():
如果request.method==“POST”:
f=请求.files['file']
f、 保存(os.path.join(上传文件夹,f.filename))
上传文件到s3(f“uploads/{f.filename}”,BUCKET)
变量_filename=f.filename
返回重定向(“/home”,变量\文件名)
非常感谢你的帮助 将变量作为url的一部分传递。更新
主页
路径和功能,如下所示-
@app.route("/home/<string:variable_filename>")
def home(variable_filename):
#*some functions here using the <variable_filename>*
return render_template('home.html')
return redirect("/home/{}".format(variable_filename))
谢谢你的回答!还想指出的是,它应该是而不是“str”: