Return 多个值 python 烧瓶
Return multiple values python flask
我正在尝试 return 一个文件并在 Flask 中更改模板。
return send_file(myfile, as_attachment=True), return render_template("template.html")
但是它给我一个错误。
如果我尝试像这样制作 2 return
s:
return send_file(myfile, as_attachment=True)
return render_template("template.html")
或
return render_template("template.html")
return send_file(myfile, as_attachment=True)
无论如何都行不通。
我该如何解决这个问题?
谢谢
每个请求只能得到一个响应。您可以发送文件,也可以发送新页面。
You only get one response per request. You can either send a file, or send a new page.
请牢记@Tim Roberts 的上述内容。
How do I send the file and then send the new page?.
A long possible ride I suggest you do something like this (Note you use another view for sending the file too)
.
# ...
if request.GET.get("send", None):
return send_file(myfile, as_attachment=True)
return render_template("template.html")
在模板中使用 ajax 向视图发出 GET 请求
ajax.get({
"/the-view-url/?send=file",
success: function(data) {
// do whatever you want with data (file)
}
多田!!您现在可以 return 回复或文件。
我建议你考虑一下:
It seems like you need to learn Python's fundamentals. –
mac13k
我正在尝试 return 一个文件并在 Flask 中更改模板。
return send_file(myfile, as_attachment=True), return render_template("template.html")
但是它给我一个错误。
如果我尝试像这样制作 2 return
s:
return send_file(myfile, as_attachment=True)
return render_template("template.html")
或
return render_template("template.html")
return send_file(myfile, as_attachment=True)
无论如何都行不通。 我该如何解决这个问题?
谢谢
每个请求只能得到一个响应。您可以发送文件,也可以发送新页面。
You only get one response per request. You can either send a file, or send a new page.
请牢记@Tim Roberts 的上述内容。
How do I send the file and then send the new page?.
A long possible ride I suggest you do something like this (Note you use another view for sending the file too)
.
# ...
if request.GET.get("send", None):
return send_file(myfile, as_attachment=True)
return render_template("template.html")
在模板中使用 ajax 向视图发出 GET 请求
ajax.get({
"/the-view-url/?send=file",
success: function(data) {
// do whatever you want with data (file)
}
多田!!您现在可以 return 回复或文件。
我建议你考虑一下:
It seems like you need to learn Python's fundamentals. – mac13k