我forloop.counter
在for
循环中应用 a以数字方式标记每个对象但我希望它仅对前五十 (50) 个对象进行编号,而忽略其余对象。这是我的代码
{% load thumbnail %}
{% for image in most_viewed %}
<div class="image">
<a href="{{ image.get_absolute_url }}">
{% thumbnail image.image "200x200" crop="10%" as im %}
<span class="forloop">**{{ forloop.counter }}**</span>
<a href="{{ image.get_absolute_url }}"> <img src="{{ im.url }}"> </a>
{% endthumbnail %}
</div>
{% endfor %}
基本上,对象 51 及以上应该没有数字
您可以尝试使用slice将for 循环重写为
{% for image in most_viewed|slice:":50" %}
它将对most_viewed 中的前 50 张图像进行迭代。
编辑 - 刚刚看到您的编辑。您可以尝试使用条件语句forloop.counter <= 50
{% for image in most_viewed %}
<div class="image">
...
{% if forloop.counter <= 50 %}
<span class="forloop">**{{ forloop.counter }}**</span>
{% endif %}
...
</div>
{% endfor %}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句