1

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.

Selcuk
  • 57,004
  • 12
  • 102
  • 110
ice916
  • 35
  • 1
  • 5

1 Answers1

2

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 %}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ThunderHorn
  • 1,975
  • 1
  • 20
  • 42