将列表的输出限制为两个项目

时间:2013-02-07 19:53:34

标签: django django-templates

如何将进入li标签的输出限制为最后两项?这是我到目前为止所做的:

{% regroup backups by dataset as dataset_list %}
{% for dataset in dataset_list %}

<h2>{{ dataset.grouper }}</h2>

<ul>
     {% for item in dataset.list %}
        <li>
            {% if item.state = "FAILED" %}<span class="icon-cross red"> {{ item.snapshot_name }} took {{ item.start_time|timesince:item.stop_time }} to transfer {{ item.transfer_size }} and <span class="red">{{ item.state }}</span>
     {% else %}
     <span class="icon-tick green"> {{ item.snapshot_name }} took {{ item.start_time|timesince:item.stop_time }} to transfer {{ item.transfer_size }}
            {% endif %}
       </li>
    {% endfor %}
    </ul>

生成如下所示的输出:

pool-test/data

    zfs-auto-snap_weekly-2013-01-06-0647 took 2 hours, 43 minutes to transfer 249GB
    zfs-auto-snap_weekly-2013-01-13-0647 took 0 minutes to transfer 29.6KB
    zfs-auto-snap_weekly-2013-01-20-0647 took 0 minutes to transfer 3.23KB
    zfs-auto-snap_weekly-2013-01-27-0647 took 0 minutes to transfer 312B
    zfs-auto-snap_weekly-2013-02-03-0647 took 0 minutes to transfer 312B

pool-test2/data

    zfs-auto-snap_weekly-2012-12-16-0647 took 13 minutes to transfer 35.9GB
    zfs-auto-snap_weekly-2012-12-23-0647 took 0 minutes to transfer 6.16MB
    zfs-auto-snap_weekly-2013-01-06-0647 took 0 minutes to transfer 22.4MB
    zfs-auto-snap_weekly-2013-01-13-0647 took 0 minutes to transfer 20.7MB
    zfs-auto-snap_weekly-2013-01-20-0647 took 0 minutes to transfer 19.1MB
    zfs-auto-snap_weekly-2013-01-27-0647 took 0 minutes to transfer 17.7MB
    zfs-auto-snap_weekly-2013-02-03-0647 took 0 minutes to transfer 10.2MB

我希望它最终看起来像:

    pool-test/data

        zfs-auto-snap_weekly-2013-01-27-0647 took 0 minutes to transfer 312B
        zfs-auto-snap_weekly-2013-02-03-0647 took 0 minutes to transfer 312B

    pool-test2/data

        zfs-auto-snap_weekly-2013-01-27-0647 took 0 minutes to transfer 17.7MB
        zfs-auto-snap_weekly-2013-02-03-0647 took 0 minutes to transfer 10.2MB

1 个答案:

答案 0 :(得分:1)

使用slice filter

{% for item in dataset.list|slice:"-2:" %}