简体   繁体   English

在Django中创建包含动态数据的3列HTML表

[英]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. 我正在尝试创建一个包含3列和3行的动态HTML表。 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). 每列将有3条来自数据库的记录,因此将显示9条记录(如果它们有9个朋友。否则只有他们有多少)。 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. 它将成为他们9个朋友的名单。 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. 我正在使用Django并且似乎无法找到一个教程,显示如果我检索9条记录,如何每行只显示3条记录。 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: 你可以使用forloop.counter并做类似的事情:

<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. here一个看起来像它做你想要的。

looks like this SO question is related: 看起来像这样的问题是相关的:

Render one queryset into 2 div columns (django template) 将一个查询集渲染为2个div列(django模板)

[EDIT] : should be "counter" not "count" [编辑]:应该“反”而不是“数”

此代码段在此上下文中也可能很有用: 将表序列分组为TABLE的行和列

您可以使用{% if friends|length == 9 %}来检查您是否有9条记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM