【问题标题】:Django Regrouping items in a table (two dimensional)Django重新组合表中的项目(二维)
【发布时间】:2014-02-07 23:08:10
【问题描述】:

我在表格中显示项目时遇到问题,这些项目都按列和行重新分组。 我写了一个应用程序,员工可以在其中记录工作日并设置状态(办公室、假期等)

我通过以下方式将全天项目传递给模板:

results = Tage.objects.filter(*args, **kwargs)

在模板中,我将列重新分组

{% regroup results|dictsort:"employee.id" by employee as employee_entries %}

然后循环遍历它们

<tr>
<th>Date</th>
{% for ma in employee_entries %}
<th>{{ ma.grouper }}</th>
{% endfor %}
</tr>

这很好用。 接下来是按天重新组合行。

{% regroup results|dictsort:"employee.id"|dictsortreversed:"starttime" by starttime|date:"d. E Y" as day_entries %}
{% for te in day_entries %}
<tr>
<th colspan="5" style="border-right: 0px;">{{ te.grouper }}</th>
</tr>

这也很好用。

现在是棘手的部分:

我想显示员工登录当天的状态:

<tr>
<th>Status</th>
{% for result in te.list %}
<th>{{ result.status }}</th>
{% empty %}
<th></th>
{% endfor %}
</tr>

如果所有员工都已在特定日期登录,那就可以了。如果某人没有像其他人一样记录一天,则所有状态列都在错误的位置。当然,这是因为 te.list 与员工列表的长度不同。

有没有人解决这样的问题?我用谷歌搜索了很多,但没有找到解决此类问题的直接解决方案。

提前致谢 康拉德

【问题讨论】:

  • 在尝试了几件事和其他人的代码之后,我认为最好在一个视图中完成。

标签: django django-templates


【解决方案1】:

我决定在视图中渲染表头和正文,并将其传递给模板并用

{{ theader|safe }}

等等。

对于 Django 模板系统的功能来说,这个问题太复杂了。

【讨论】:

    猜你喜欢
    • 2013-02-01
    • 2021-10-23
    • 1970-01-01
    • 2019-10-24
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    相关资源
    最近更新 更多