如何反向计算列表(从n到1)

최연석*_*최연석 2 django django-templates

我的模板代码是这个

{% for announcement in announcements %}
    <tr>
        <td>{{ count }}</td>
        <td>{{ announcement.title }}</td>
        <td>{{ announcement.user.profile.name }}</td>
        <td>{{ announcement.modified }}</td>
    </tr>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

我想将公告数从n写入1。

我怎样才能做到这一点?

Raf*_*ael 5

有一个反向计数器称为forloop.revcounter

{% for announcement in announcements %}
    <tr>
        <td>{{ forloop.revcounter }}</td>
        <td>{{ announcement.title }}</td>
        <td>{{ announcement.user.profile.name }}</td>
        <td>{{ announcement.modified }}</td>
    </tr>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

Django模板“用于”文档