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!
            Asked
            
        
        
            Active
            
        
            Viewed 1.2k times
        
    3 Answers
9
            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"
1
            
            
        This snippet might also be useful in this context: Group sequence into rows and columns for a TABLE
 
    
    
        arie
        
- 18,737
- 5
- 70
- 76
 
     
     
     
    
{{ f.to_friend.username }}