【发布时间】:2019-12-16 17:37:27
【问题描述】:
我正在处理一个动态表,其中一些条目是通过简单的 Python 连接从数据库中提取的。我正在使用 Flask 和 Jinja 来渲染 HTML。
为了实现这一点,我使用了 cur.fetchall():
cur.execute('SELECT * FROM conceptos WHERE conceptos.conceptoTipo = 1')
data = cur.fetchall()
但是,我有兴趣修改每个条目的特定值,在本例中是第 9 位的值。为此,我创建了一个列表并使用 for 去通过data中的条目,像这样:
x = []
for i in data:
x.append(i[9]+1) #Just adding 1 to the numeric value for testing the change.
最后,我用 render_template 返回两个变量:
return render_template('descuentosBeneficiarios.html', conceptos = data, operas = x)
在 HTML 中,我使用 Jinja2 在一个简单的表格中动态地公开变量:
{% for concepto in conceptos %}
<tr>
<!-- Recorrer Campos de Tabla-->
<td>{{concepto.0}}</td>
<td>{{operas}}</td>
<td>{{concepto.2}}</td>
<td>{{concepto.3}}</td>
<td>{{concepto.4}}</td>
<td>{{concepto.5}}</td>
<td>{{concepto.6}}</td>
<td>{{concepto.7}}</td>
<td>{{concepto.8}}</td>
<td>{{concepto.9}}</td>
<td>{{concepto.10}}</td>
<td>{{concepto.11}}</td>
<td>
</tr>
{% endfor %}
因此,对于每一行,我都会在第二列的 operas 列表中获取所有值,而我只需要第一行的第一个,第二行的第二个,等等,但我不知道该怎么做。
感谢一切!
【问题讨论】:
标签: python html for-loop flask