如何在django中将views.py中的数据帧渲染为html模板



我正在用Django构建一个网络应用程序,其中一个功能是用户可以上传数据集并在另一个页面上查看。我目前只尝试从文件路径读取数据集,并将其显示在另一个页面中;我已经将测试.csv放在了要读取的文件中,但我一直收到错误"列表"对象没有属性"到html">

以下是视图。py

path = r"C:/Users/user/Documents/django_saved_files/"

path1, dirs, files = next(os.walk(path))
file_count = len(files)
dataframes_list = []
for i in range(file_count):
temp_df = pd.read_csv(path+files[i])
dataframes_list.append(temp_df)
dataframes_list_html = dataframes_list.to_html(index=False)
return render(request,'blog/view_datasets.html',{'Dataframe':dataframes_list_html})

这是HTML模板:

<body>
<div class="container">
<h1 class="section-header">Datasets Available</h1><hr>
<div class="content-section">
Output: {{Dataframe|safe}}
</div>
</div>
</body>

创建HTML数据列表,而不是使用的数据帧

dataframes_list_html = []
for i in range(file_count):
temp_df = pd.read_csv(path+files[i])
dataframes_list_html.append(temp_df.to_html(index=False))
return render(request,'blog/view_datasets.html',{'dataframes': dataframes_list_html})

然后在模板中的列表上枚举并呈现数据帧:

<div class="content-section">
{% fordataframe in dataframes%}
Output: {{dataframe|safe }}
{% endfor %}
</div>

最新更新