python-flask render_template和另一个返回值



我的web应用程序使用Python烧瓶。应用程序提供要下载的CSV文件。CSV文件是下面代码块中的响应。此外,我还需要向html模板发送一个变量。我怎么能有两个返回值?

@application.route("/log_analysis", methods=['POST'])
def get_response():
output='The result of your query :  '+str(i-1)+' . The full report is downloaded automatically.'
cw.writerows(csv_rows)
response = make_response(si.getvalue())
response.headers["Content-Disposition"] = f"attachment; filename={return_file_name}"
response.headers["Content-type"] = "text/csv"

return render_template('base.html',output=output)
return response, 200

输出将显示在html中,但第二个返回中的响应不起作用。

看完你的问题后,我想你想要的是类似于flash消息的东西。您传入的变量content只是文本,用于显示消息。


闪存信息

您需要在base.html或正在渲染的任何模板中进行设置。

样板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="{{ url_for('static', filename='css/main.css')}}" rel="stylesheet">
</head>
<body>
<main>
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="alert alert-{{ category }}">
{{ message }}
</div>
{% endfor %}
{% endif %}
{% endwith %}
{% block content %}{% endblock %}
</main>
</body>
</html>

from flask import render_template, url_for, flash, redirect
@application.route("/log_analysis", methods=['POST'])
def get_response():
output=f'The result of your query :  {i-1} . The full report is downloaded automatically.'
cw.writerows(csv_rows)
response = make_response(si.getvalue())
response.headers["Content-Disposition"] = f"attachment; filename={return_file_name}"
response.headers["Content-type"] = "text/csv"
flash(output,'success')

return response, 200

您也可以尝试在html模板中执行类似alerts的操作

相关内容

最新更新