使用Django模板到HTML表

时间:2018-08-02 04:42:22

标签: html django

我想设计一个可以按年相互比较的表。

我的数据是这样的:

book Data Image

# 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())

我希望结果是想要的

enter image description here

我尝试过的Django模板重新组合的内容 https://docs.djangoproject.com/en/2.0/ref/templates/builtins/#regroup

但不能满足我的要求。

1 个答案:

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