繁体   English   中英

Django阅读返回的每个项目的索引

[英]Django reading index of each item returned

我将在模板中的标准for循环中返回项目列表。 可以获取返回的每个项目的索引值。

{% for entry in latest_entries %}
    <li class="list-item">
        <button class="button button-circle"><span class="list-index">1</span></button>
        <a class="SidebarLatestTitle" href="{{ entry.get_absolute_url }}">{{ entry.title }}</a>
    </li>
 {% endfor %}

我希望按钮中的硬编码“ 1”在列表中返回正确的数字。 如果返回10个结果,则为(1-10)。 Django是否提供访问此号码的方法?

听起来您正在询问forloop.counter

forloop.counter循环的当前迭代(1索引)

{% for entry in latest_entries %}
    <li class="list-item">
        <button class="button button-circle"><span class="list-index">{{ forloop.counter }}</span></button>
        <a class="SidebarLatestTitle" href="{{ entry.get_absolute_url }}">{{ entry.title }}</a>
    </li>
{% endfor %}

https://docs.djangoproject.com/zh-CN/1.7/ref/templates/builtins/#for

使用神奇的forloop.counterforloop.counter0

暂无
暂无

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

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