2015-10-13 24 views
1

我正在FLASK中使用對象數組製作表格。我想在每個表格行顯示4個對象,但batch(4)命令似乎沒有正常工作。它運行,沒有錯誤。但是什麼都不顯示。燒瓶每個表格行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> 

我很感謝在這件事上的任何幫助。

回答

1

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 %}