Creating a table with three columns of HTML with dynamic data in Django

I am trying to create a dynamic HTML table with three columns and three rows. Each column will have 3 records from the database, so 9 total records will be displayed (if they have 9 friends, otherwise only their number). I do this to basically display small user profiles with the usernames of my friends on the users homepage. This will be a list of 9 of your friends. I am using Django and cannot find a tutorial showing how to display only 3 entries per line if I get 9 shared entries. Any help would be appreciated, be it a tutorial link or information on how to solve this problem. Thanks!

+6
source share
3 answers

you can use forloop.counter and do something like:

 <table> <tr> {% for person in people %} <td>{{ person }}</td> {% if not forloop.last and forloop.counter == 3 or forloop.counter == 6 %} </tr> <tr> {% endif %} {% endfor %} </tr> </table> 

or collapse your own template tag. One here looks like it is doing what you want.

it looks like this SO question is related:

Mark one query in 2 div columns (django template)

[EDIT]: Must be a “counter”, not a “count”

+7
source

This snippet may also be useful in this context: Group sequence in rows and columns for TABLES

+1
source

you can use {% if friends|length == 9 %} to check if you have 9 entries.

0
source

Source: https://habr.com/ru/post/886675/


All Articles