Is a django template forloop resetabble?
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 %}
It sounds like you should be regrouping objects based on type, and doing the padding in the inner loop.
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.
精彩评论