简体   繁体   中英

Using django template to html table

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

my data like this:

图书数据图片

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

I hope result want:

在此处输入图片说明

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

but can't achieve my require.

In the django template

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM