使用 For 循环在 Flask 模板中迭代多个嵌套列表

Iterate Multiple Nested Lists in Flask Template using For loop

我有如下 Python 列表

[[1, 'sarmad ali', 10], [2, 'nabeel', 200], [3, ' tayyab', 40202]]

我想在模板(html 页面)中以 table/two 维度样式显示它们

1 sarmad ali 10

2 nabeel 200

3 tayyab 40202

我能够得到以下

1 sarmad ali 10 2 nabeel 200 3 tayyab 40202

在我的 python 文件中,我的数据格式为 Dataframe,并转换为 list

{% for col in data_list%}
    
    {% for row in col%}
        
        <td>{{row}}</td>
    
    {% endfor %}
    
    <br>

{% endfor %}

上面的嵌套循环以单行而不是 table 格式生成输出。 我发现第二个循环是在单行中迭代所有列表而不是一次迭代单个列表

如有任何问题,请随时提出更多说明。

<table>
{% for sno, name, rank in data_list%}
   <tr>
     <td>{{sno}}</td>
     <td>{{name}}</td>
     <td>{{rank}}</td>
  </tr>
{% endfor %}
</table>

已编辑: 假设如果你不知道列表中的项目数,

<table>
{% for tr in data_list%}
   <tr>
   {% for td in data_list%}
     <td>{{td}}</td>
    {% endfor%}
  </tr>
{% endfor %}
</table>

这是我在模板文件中用来显示列表的内容。

<div class="content-section">
    <legend class="mb-4"> Table </legend>
<table class="table" >
<thead>
    <tr>
      <th scope="col">#</th>
      {%for i in columns%}
      <th scope="col">{{i[0]}}</th>
      {%endfor%}
    </tr>
  </thead>
  <tbody>

    {%for i in result%}

    <tr>
          <th scope="row">{{loop.index}}</th>
          {%for k in i%}
          <td>{{k}}</td>
          {%endfor%}
      </tr>

    {%endfor%}
    </tbody>
</table>

</div>