开发者

Creating a 3 column HTML table with dynamic data in Django

开发者 https://www.devze.com 2023-02-28 21:30 出处:网络
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 ju

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 howe开发者_如何学运维ver 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!


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"


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消