【问题标题】:Read a CSV file in Flask and display values from the CSV file in an HTML table在 Flask 中读取 CSV 文件并在 HTML 表格中显示 CSV 文件中的值
【发布时间】:2021-10-10 20:02:26
【问题描述】:

我正在尝试使用 Flask 在我的 Web 应用程序上显示来自 CSV 文件的数据。在下面的代码中,我试图从我的 CSV 文件中选择 5 列,并在 HTML 表中显示这些列的值。不幸的是,出现在 HTML 表中的值是 {{ value [0] }}, {, {{ value [1] }}, { value [2] }}, {{ value [3] }}, {{值 [4] }},{{ 值 [5] }}。

我的python函数:

@app.route("/homepage")
def table_issues():
columns=["Regulatory_Domain","Detection_Date","ID_Client","Issue_ID_Name","Status_Issue","Comments"]
df = pd.read_csv("data.csv", names=columns, header= 0)
df = df.sort_values(by=['Detection_Date'], ascending = True)
issueslist = list (df.values)
return render_template("homepage.html", issueslist=issueslist)

我的 HTML 代码:

        <table>
        <thead class=theadissues> 
            <tr>
                <th>Regulatory Domain</th>
                <th>Detection Date</th>
                <th>Client-ID</th>
                <th>Issue ID / Name</th>
                <th>Review Status</th>
                <th>Comments</th>
                <th>Edit</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>{{ value[0] }}</td>
                <td>{{ value[1] }}</td>
                <td>{{ value[2] }}</td>
                <td>{{ value[3] }}</td>
                <td>{{ value[4] }}</td>
                <td>{{ value[5] }}</td>
                <td>
                    <p class="EditAction"><a href="/updateform">EDIT ISSUE</a></p>
            </tr>
        </tbody>
    </table>

您能帮我更正我上面的代码吗?

非常感谢您的帮助。

【问题讨论】:

    标签: python html csv flask


    【解决方案1】:

    将“价值”一词更改为“问题列表”

    当您渲染模板时,您将一个列表传递给它...

    return render_template("homepage.html", issueslist=issueslist)
    

    所以现在你的 html 模板可以访问同一个变量

    <td>{{ issueslist[0] }}</td>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-05
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 2016-07-06
      • 2011-03-29
      • 1970-01-01
      相关资源
      最近更新 更多