【发布时间】:2020-03-07 03:42:43
【问题描述】:
我的应用有一个奇怪的问题。
我正在做的是: 我在 JS(客户端)中生成一个 csv 文件,然后将其发送到烧瓶,然后我使用 python 脚本过滤文件,然后将其返回给客户端。
除了最后一部分,一切都很好。当涉及到 return render_template 时,它只是跳过了该部分。这很奇怪,因为它进入了 if request.method == 'POST'。我在 if request.method=='POST' 中打印了值,我可以在服务器端看到这些值。
烧瓶路由.py:
@app.route('/update_file', methods=['GET', 'POST'])
@login_required
def update_file():
'''Opens the filtered_file page but with updated file'''
clicked = None
if request.method == 'POST':
clicked = io.StringIO(request.form['data'])
file_to_filter = pd.read_csv(clicked, sep=',', engine='python', encoding='utf_8_sig')
table1 = update_csv(file_to_filter)
print(table1)
table2 = table1.to_html(classes='my_class" id = "my_id')
return render_template('3_filtered_file.html', data=table2)
这就是我在我的 html 模板上显示它的方式:
<div class="table-responsive">
{{data | safe}}
</div>
我已经对客户端上传的文件做了类似的处理,效果很好,但是这个有一个我似乎找不到的错误:/
编辑: 这是我发送 ajax 请求的 JS:
//On Update click renders table to csv, activates the be_filter and reopens it in the filtered_file.html
var isClicked;
jQuery("#update").on('click', function(){
var response = confirm('Are you sure you want to UPDATE rows ?');
if(response == true){
isClicked = $('#my_id').table2csv();
$.ajax({
type:'POST',
url:"{{url_for('update_file')}}",
data: {'data': isClicked}
});
//window.location.href='/update_file';
}else{
return false;
}
});
【问题讨论】:
-
这很奇怪。我以为这也解决了。你的 table1.to_html 做什么?应该是 table1.to_html(classes="my_class", id = "my_id") 吗?
-
您的 3_filtered_file.html 在哪里?它在模板文件夹中吗?
-
是的,它在模板文件夹中
-
在 POST 请求/表单提交后按 Ctrl+U 时,您在浏览器中究竟看到了什么?有什么渲染的吗?你能在源代码中看到
<div class="table-responsive">吗?您是否检查了浏览器控制台的错误消息? -
to_html 只是一个函数,它接受 csv 文件并将其呈现为 html 表,以便您可以在 html 中打开它。我已经为上传的文件做了这个并且它有效
标签: javascript python ajax flask