【问题标题】:Python-Django: user info in templatePython-Django:模板中的用户信息
【发布时间】:2026-01-16 19:25:02
【问题描述】:

我有数据库,目前在我的数据库中,它在我的模板(HTML 表格)页面中显示如下。

sno--scene--frames--empID

1------001----24-----100

2------002----34-----100

3------003----20-----101

4------004----15-----101

5------005----10-----100

但我想这样显示(如下)。如何从HTML(tables) 获取此信息。我正在使用 Python-Django。

sno---scene---frames---empID

1--------001-----24-------100

2--------002-----34-------100

3--------005-----10-------100

------------- tot=68

1------003-----20--------101

2------004-----15--------101

-------------tot=35 

【问题讨论】:

    标签: python django templates


    【解决方案1】:

    您应该在视图中准备数据 - 按 id 分组,计算总和。结果 cal 如下所示:

    items_info = [{'items':[item1, item2, item3], 'total': 68}, {'items':[item4, item5], 'total': 35}]
    

    那么模板应该是这样的:

    {% for info in items_info %}
        {% for item in info.items %}
            <tr> 
                <td>{{ forloop.counter }}</td>
                <td>{{ item.scene }}</td>
                <td>{{ item.frames }}</td>
                <td>{{ item.id }}</td>
            </tr>
        {% endfor %}
        <tr>
            <td colspan="3">TOTAL</td>
            <td>{{ info.total }}
        </tr>
    {% endfor %}
    

    希望这会有所帮助!

    【讨论】: