简体   繁体   English

如何将数据框从 views.py 渲染到 django 中的 html 模板?

[英]How to render a data frame from views.py to an html template in django?

I'm building a webapp with Django, and one of the features is that the user can upload a dataset and view it on another page.我正在使用 Django 构建一个网络应用程序,其中一个功能是用户可以上传数据集并在另一个页面上查看它。 I'm currently only attempting to read a dataset from a file path and display it in another page;我目前只是试图从文件路径中读取数据集并将其显示在另一个页面中; I've placed my test.csv in the file that I want to read from, but I keep getting the error 'list' object has no attribute 'to html' .我已将我的test.csv放在我想要读取的文件中,但我不断收到错误'list' object has no attribute 'to html'

Here is the views.py :这是views.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})

and Here is the HTML Template :这是HTML 模板

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

Create a list of HTML data instead of the dataframes with:使用以下命令创建 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})

and then enumerate over the list in the template and render the dataframes:然后枚举模板中的列表并呈现数据帧:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM