Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a 3 column HTML table with dynamic data in Django

I am trying to create a dynamic HTML table with 3 columns and 3 rows. Each column will have 3 records from the database so there will be 9 total records displayed (if they have 9 friends. otherwise just however many they have). I am doing this to mainly display small user profile pictures with their friends' usernames on the users homepage. Its going to be a list of 9 of their friends. I am using Django and cannot seem to find a tutorial showing how to display only 3 records per row if I retrieve 9 total records. Any help would be appreciated whether it be a link to a tutorial or info on how to solve this issue. Thanks!

like image 526
vt-cwalker Avatar asked Apr 26 '11 02:04

vt-cwalker


2 Answers

you can use the 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 roll a custom template tag. One here looks like its doing what you want.

looks like this SO question is related:

Render one queryset into 2 div columns (django template)

[EDIT] : should be "counter" not "count"

like image 129
dting Avatar answered Nov 02 '22 19:11

dting


This snippet might also be useful in this context: Group sequence into rows and columns for a TABLE

like image 31
arie Avatar answered Nov 02 '22 19:11

arie