我正在 FLASK 中使用对象数组制作一个表格。我想在每个表行显示 4 个对象,但 batch(4)
命令似乎无法正常工作。它运行,没有错误。但也没有显示任何内容。
<table class="Fruits_n_Veggies">
{% for item in fruit | batch(4) %}
{% if item.name %}
<tr>
<td class = "img"> <img src="{{ url_for('static', filename=item.img_url) }}" height="100"; width="100"><br>
{{ item.name }} {{ item.price_min }} - {{ item.price_max }}</td>
</tr>
{% endif %}
{% endfor %}
</table>
如果对此事有任何帮助,我将不胜感激。
最佳答案
batch
返回一个包含 4 个对象的容器。您还需要迭代它们。
{% for row in fruit | batch(4) %}
<tr>
{% for item in row %}
<td class = "img"> <img src="{{ url_for('static', filename=item.img_url) }}" height="100"; width="100"><br>
{{ item.name }} {{ item.price_min }} - {{ item.price_max }}</td>
{% endfor %}
</tr>
{% endfor %}
关于python - 每表行 flask 4 个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33101897/