我想设计一个可以按年相互比较的表。
我的数据是这样的:
# views.py
def bybook(request, bookName='A-Book'):
bookdata = models.bookdb.objects.filter(bookName=bookName).order_by('Year', 'point')
return render(request, 'book.html', locals())
我希望结果是想要的
我尝试过的Django模板重新组合的内容 https://docs.djangoproject.com/en/2.0/ref/templates/builtins/#regroup
但不能满足我的要求。
答案 0 :(得分:0)
在Django模板中
#first we create the header of the table with the years
<tr class="first-table-row">
# leave it blank so the first column is empty
<th></th>
#loop trough the items
{% for i in bookdata %}
<th>{{i.year}}</th>
{% endfor %}
</tr>
#loop trough the items again
{% for i in bookdata %}
<td>{{i.point}}</td>
# I don't know how your object is structured man please submit it so I can continue
{% endfor %}