繁体   English   中英

使用render_template()中的'for'循环在html中显示表

[英]displaying table in html using 'for' loop from render_template()

使用Python Flask应用程序使用'for'循环实现呈现html表。 但是数据不会呈现空白<tr><tr>

我通过从render_template传递dff.to_html()并在'for'循环中检索html中的表来显示,从而将数据帧转换为html。

Python代码:

return render_template('index.html', table = dff.head(50).to_html())

HTML代码:

         <table>
            <tbody>

            {% for row in table %}
                <tr>
                    <td>{{row.name}}</td>
                    <td>{{row.link_to_detail}}</td>
                    <td>{{row.link}}</td>
                    <td><input name ="get poster"  type="submit" value={{row['id']}}></td>
                </tr>
            {% endfor %}
            </tbody>
        </table>

实际结果是

<tr>
      <td></td>
      <td></td>
      <td></td>
      <td><input name="get poster" type="submit" value=""></td>
</tr>

预期结果 :

    <tr>
      <td>name</td>
      <td>link_to_detail</td>
      <td>link</td>
      <td><input name="get poster" type="submit" value=""></td>
    </tr>

“””

dff.head(50).to_html()返回一个html表,而不是一个python对象。

这个html表实际上是一个字符串,你正在迭代字符( row ),它没有任何名为namelinklink_to_detail属性......

如果您发布dff数据帧的样本,我将能够完成此答案。

我从来没有工作python,但想法是你需要发送变量来查看。

return render_template('index.html', table = dff.head(50).to_html(),  name='name' ,row.link_to_detail= 'row.link_to_detail' , link ='link')

它可以是这样的

不要使用dff.head().to_html()返回HTML表作为字符串,而是dff[:50].values (给出前50行所有列的2d数组)然后row.0row.1获取每行row.1两列的值。

暂无
暂无

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

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