使用Django模板到html表 [英] Using django template to html table

查看:33
本文介绍了使用Django模板到html表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to design a table can compare with each other by year.

我的数据是这样的:

# 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

What i have tryed django templates regroup https://docs.djangoproject.com/en/2.0/ref/templates/builtins/#regroup

但不能满足我的要求.

推荐答案

在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 %}

这篇关于使用Django模板到html表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆