python - Jinja2 loop creating duplicates
问题描述
I am trying to create a table using data from a few arrays that I have for my Flask webapp.
I am using two loops -- first to loop through the variables to create the rows; second to loop through the array itself to get the different positions. This works for the most part but it is looping too much? My results are the same thing just 3 times.
<table id="example-datatable" class="table table-bordered table-hover">
<thead>
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Price</th>
<th>Discount %</th>
</tr>
</thead>
<tbody>
{% for record in invoice %}
{% set length = (invoice | length)-1 %}
{% for i in range(0, length) %}
<tr>
<td>{{invoice["fields"]["Account item codes"][i]}}</td>
<td>{{invoice["fields"]["Account item quantities"][i]}}</td>
<td>{{invoice["fields"]["Account item prices"][i]}}</td>
<td>{{invoice["fields"]["Account item discount %s"][i]}}</td>
</tr>
{% endfor %}
{% endfor %}
</tbody>
These are the results I get. It's correct, but just replicating three times and not just once.
I am to the point now where I think I have looked at it so much that I am blind to the problem.
解决方案
能够通过取出一个循环来修复它......愚蠢的修复。但很高兴把它修好!
编辑代码:
<table id="example-datatable" class="table table-bordered table-hover">
<thead>
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Price</th>
<th>Discount %</th>
</tr>
</thead>
<tbody>
{% set length = (invoice | length)-1 %}
{% for i in range(0, length) %}
<tr>
<td>{{invoice["fields"]["Account item codes"][i]}}</td>
<td>{{invoice["fields"]["Account item quantities"][i]}}</td>
<td>{{invoice["fields"]["Account item prices"][i]}}</td>
<td>{{invoice["fields"]["Account item discount %s"][i]}}</td>
</tr>
{% endfor %}
</tbody>
</table>
推荐阅读
- python - 如果相关项目的 COUNT 个,您如何编写 Django 查询条件?
- ios - Swift - 仅对特定约束进行动画处理
- sql - 员工维度在数据仓库中每天被截断
- twilio - 将 Twilio MMS 媒体文件复制到我的服务器
- raku - 多行定义或“//”
- php - 如何通过多态关系将关系写入到远程模型
- javascript - 如何解决 React-Native 中 Modal 中两个组件之间的边距问题?
- python-3.x - 使用 Python 更新 Sqlite:InterfaceError:错误绑定参数 0 和 None 类型不可下标
- unity3d - 如何在 Unity 中从高处坠落时使板条箱破裂/破碎
- android - 如何修复sqlite查询错误在哪里?不会被选择参数替换