1
我有這個名單這與Django的做多本字典的從查詢:從詞典列表生成Django的多行/列的表
對多種樂器的統計數據對每個會話強硬的聚合在一所音樂學校。
[{'count': 13, 'session': 1, 'instrument': u'Piano'}, {'count': 4, 'session': 1, 'instrument': u'Cello'}, {'count': 2, 'session': 1, 'instrument': u'Violin'}, {'count': 14, 'session': 2, 'instrument': u'Piano'}, {'count': 1, 'session': 4, 'instrument': u'Cello'}]
如何可以循環這個通過作出表4列和n行,知道會有永遠4屆,但intruments的n個?
我幾乎嘗試了輸出什麼,我想(我的模板):
<table class="table">
<thead>
<tr>
<th>Session 1</th>
<th>Session 2</th>
<th>Session 3</th>
<th>Session 4</th>
</tr>
</thead>
{% for stat in stats_instrument %}
{% if stat.session == 1 %}
<tr><td>{{ stat.instrument }} {{ stat.count }}</td></tr>
{% endif %}
{% endfor %}
{% for stat in stats_instrument %}
{% if stat.session == 2 %}
<tr><td></td><td>{{ stat.instrument }} {{ stat.count }}</td></tr>
{% endif %}
{% endfor %}
...
</table>
實際的輸出是:
|Session 1| Session 2| Session 3| Session 4|
|Piano 13 |
|Cello 4 |
|Violin 2 |
| | Piano 14 |
| | | | Cello 1 |
我要的是更喜歡:
|Session 1| Session 2| Session 3| Session 4|
|Piano 13 | Piano 14 | | Cello 1 |
|Cello 4 |
|Violin 2 |
我看到3個問題。
- 這些樂器在每個環節都不一樣,所以鋼琴可以在1-2-3會話中教。
- 會議不具有相同數量的樂器,會議1可以具有約5個樂器和會話3約10個樂器。
- 我正在循環多次相同的數據,它似乎不是非常有效。
我應該製作4個表格並放置類似內嵌塊的東西嗎?
它工作。我在修訂版中添加了表格的結束標籤。 – metraon 2015-01-16 14:59:49