【发布时间】:2014-07-21 14:06:11
【问题描述】:
所以我有这本字典:
[{'name': 'Strom', 'jahr': 2014, 'summe': 1258.0}, {'name': 'Strom', 'jahr': 2013, 'summe': 0}, {' name':'Strom','jahr':2012,'summe':0},{'name':'Erdgas','jahr':2014,'summe':1425.7485714285715},{'name':'Erdgas' , 'jahr': 2013, 'summe': 0}, {'name': 'Erdgas', 'jahr': 2012, 'summe': 0}]
我想把它放到一张桌子上:
Energy 2014 2013 2012
Strom 1258 0 0
Erdgas 1425.74 0 0
将始终显示 3 年,但可能超过 2 行。
我不太擅长 CSS 和 HTML,所以它主要是尝试和错误,这是我目前的 html 文件:
{% extends 'base.html' %}
{% load verbrauchererfassung_tags %}
{% block content %}
{{Verbrauch}}
<h1 class="page-header">Verbraucheranzeige</h1>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>Energieträger</th>
{% for y in Years %}
<th>{{ y }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for v in Verbrauch %}
{% if forloop.counter0|divisibleby:3 %}
<tr>
<td>{{v.name}}</td>
{% endif %}
{% for verbrauch in Verbrauch %}
<td>{{verbrauch.summe}} </td>
{% if forloop.counter|divisibleby:3 or forloop.last%}</tr>{% endif %}
{% endfor %}
{% endfor %}
</table>
{% endblock %}
看起来像这样:
Energieträger 2014 2013 2012
Strom 1258,0 0 0
1425,7485714285715 0 0
1258,0 0 0
1425,7485714285715 0 0
1258,0 0 0
1425,7485714285715 0 0
Erdgas 1258,0 0 0
1425,7485714285715 0 0
1258,0 0 0
1425,7485714285715 0 0
1258,0 0 0
1425,7485714285715 0 0
是的,这让我发疯,我想我可能在这里走错路了,你们能帮帮我吗?
编辑:
所以在休息了很长时间后,我决定这样订购我的字典:
[{'name': 'Strom', 2: 2013.0, 3: 2012.0, 1: 1258.0}, {'name': 'Erdgas', 2: 0, 3: 0, 1: 1425.7485714285715}]
这导致了一个模板:
{% extends 'base.html' %}
{% load verbrauchererfassung_tags %}
{% block content %}
{{Verbrauch}}
<h1 class="page-header">Verbraucheranzeige</h1>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>Energieträger</th>
{% for y in Years %}
<th>{{ y }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for v in Verbrauch %}
{% if forloop.counter0|divisibleby:3 %}
<tr>
{% endif %}
<td>{{v.name}}</td>
<td>{{v.1}}</td>
<td>{{v.2}}</td>
<td>{{v.3}}</td>
{% if forloop.counter0|divisibleby:"3" or forloop.last%}</tr>{% endif %}
{% endfor %}
</table>
{% endblock %}
还有这张漂亮的桌子(我根据年份做了数值,所以我可以看看我的顺序是否正确)
Energietraeger 2014 2013 2012
Strom 1258,0 2013,0 2012,0
Erdgas 1425,74 0 0
【问题讨论】:
-
这里:
{% if forloop.counter0|divisibleby:3 %}<tr>- 移动endif之前td
标签: django templates python-3.x