ice916
ice916

Reputation: 45

Using django template to html table

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

my data like this:

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

I hope result want:

enter image description here

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

but can't achieve my require.

Upvotes: 1

Views: 4400

Answers (1)

ThunderHorn
ThunderHorn

Reputation: 2035

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

Upvotes: 2

Related Questions