django 模板 forloop 可以重置吗?
是否可以将 forloop.counter 对象重置为零?
我用它来吐出对象,但是当对象类型发生变化时,我想将其归零,这样我就可以数到 5 个对象,并输出行尾对象(没有右填充)作为第五个也是唯一的第五个对象在那种类型中。
如果我有 5 个对象,前两个是 a 类型,接下来的 3 个是 b 类型,第五个总是会得到较小的填充,不用介意它只是线上的第三个。
这是我的模板代码:
{% for project in projects %}
{% ifchanged project.proj_type %}
{% forloop.counter=="1" %}
</div>
<div class="span-4 prepend-top">
<h5 class="right" >{{ project.proj_type }}</h5>
</div>
<div class="span-19 append-1 last" id="row-of-projects">
{% endifchanged %}
{% if forloop.counter|divisibleby:"4" %}
<div class="span-4 append-1 prepend-top last" id="project">
{% else %}
<div class="span-4 append-1 prepend-top" id="project">
{% endif %}
<p class="project-name">{{ project.name }}</p>
<a href="/gallery/{{ project.slug }}/" ><img src="/media/pa/photographs/{{ project.get_photograph }}-t.jpg" alt="{{ project.name }}" /></a>
</div id="project">
{% endfor %}
Is it possible to reset the forloop.counter object back to zero?
I'm using it to spit out objects, but when the object type changes I want to zero it, so that I can count to 5 objects and output an end of row object (with no right padding) as the fifth and only fifth object in that type.
And if I've got 5 objects, the first two are type a and the next 3 are type b, the fifth one will always get the smaller padding, never mind that its only the third on the line.
Here is my template code:
{% for project in projects %}
{% ifchanged project.proj_type %}
{% forloop.counter=="1" %}
</div>
<div class="span-4 prepend-top">
<h5 class="right" >{{ project.proj_type }}</h5>
</div>
<div class="span-19 append-1 last" id="row-of-projects">
{% endifchanged %}
{% if forloop.counter|divisibleby:"4" %}
<div class="span-4 append-1 prepend-top last" id="project">
{% else %}
<div class="span-4 append-1 prepend-top" id="project">
{% endif %}
<p class="project-name">{{ project.name }}</p>
<a href="/gallery/{{ project.slug }}/" ><img src="/media/pa/photographs/{{ project.get_photograph }}-t.jpg" alt="{{ project.name }}" /></a>
</div id="project">
{% endfor %}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来你应该根据类型重新分组对象,并在内循环中进行填充。
It sounds like you should be regrouping objects based on type, and doing the padding in the inner loop.
我不太确定我是否理解您的需求。
您可以使用
regroup
标签根据类型显示对象。您可以使用
class="{%cycle 'normal' 'normal' 'normal' 'normal' 'special' %}"
为每五个元素设置不同的样式。I'm not quite sure that I understand what you need.
You can use the
regroup
tag to display your objects according to type.And you can use
class="{% cycle 'normal' 'normal' 'normal' 'normal' 'special' %}"
to style every fifth element differently.