我有3個字典數據['d1'],data ['d2']和data ['d3']。Django模板表
我要的是表看起來像這樣,從每個字典獲取鍵和展示他們像:
Team 1 | Team 2 | Team 3
key 1 from d1 | key 1 from d2 | key 1 from d3
key 2 from d1 | key 2 from d2 | key 2 from d3
key 3 from d1 | key 3 from d2 | key 3 from d3
key 4 from d1 | key 4 from d2 | key 4 from d3
我有什麼tryed
<thead>
<tr>
<th>Team 1</th>
<th>Team 2</th>
<th>Team 3</th>
</tr>
</thead>
<tbody>
<tr>
{% for key, value in d1.items %}
<td>{{ key }}</td>
{% endfor %}
</tr>
<tr>
{% for key, value in d2.items %}
<td>{{ key }}</td>
{% endfor %}
</tr>
<tr>
{% for key, value in d3.items %}
<td>{{ key }}</td>
{% endfor %}
</tr>
</tbody>
或
<thead>
<tr>
<th>Team 1</th>
<th>Team 2</th>
<th>Team 3</th>
</tr>
</thead>
<tbody>
{% for key, value in d1.items %}
<tr>
<td>{{ key }}</td>
</tr>
{% endfor %}
{% for key, value in d2.items %}
<tr>
<td>{{ key }}</td>
</tr>
{% endfor %}
{% for key, value in d3.items %}
<tr>
<td>{{ key }}</td>
</tr>
{% endfor %}
</tbody>
但它顯示不同。我怎樣才能達到上述表格?
當前表顯示如下:
Stalone Sobont arda AAA
Sardina Jhonny Lala Vali
LilJon Scuipy SSS Mazare
views.py
stats = data['scoruri'].copy()
data['d1'] = {}
data['d2'] = {}
data['d3'] = {}
for key, value in stats.copy().items():
if not data['d1'] or sum(data['d1'].values()) <= sum(data['d2'].values()) and \
sum(data['d1'].values()) <= sum(data['d3'].values()):
name, score = max(stats.items(), key=lambda p: p[1])
data['d1'][name] = score
stats.pop(name, score)
data['total_1'] = sum(data['d1'].values())
elif not data['d2'] or sum(data['d2'].values()) <= sum(data['d1'].values()) and \
sum(data['d2'].values()) <= sum(data['d3'].values()):
name, score = max(stats.items(), key=lambda p: p[1])
data['d2'][name] = score
stats.pop(name, score)
data['total_2'] = sum(data['d2'].values())
elif not data['d3'] or sum(data['d3'].values()) <= sum(data['d1'].values()) and \
sum(data['d3'].values()) <= sum(data['d2'].values()):
name, score = max(stats.items(), key=lambda p: p[1])
data['d3'][name] = score
stats.pop(name, score)
data['total_3'] = sum(data['d3'].values())
它是什麼當前顯示(當前表格佈局)? –
你能展示你如何在視圖中填充d1,d2和d3嗎? –
完成後,找你自己 –