5π
β
You canβt do evaluations like that in the django template. The ifequal tag expects only two parameters, and compares them. You would need some type of filter.
However, you could use the cycle tag instead:
{% for p in plist %}
{% if forloop.first %}
<li> {{p.title}} </li>
{% else %}
<li{% cycle '' '' '' ' class="clear"' %}> {{p.title}} </li>
{% endif %}
{% endfor %}
EDIT: As pointed out, the original solution cleared the 4, 8th, etc, instead of from the 5th onwards. I have updated the answer to include the changes by Tolga.
π€Andre Miller
12π
You can use forloop.counter0 and filter divisibleby:
{% ifequal forloop.counter0|divisibleby:"4" %}
π€Griffosx
- [Django]-Django-compressor + less compress the files but link to original files
- [Django]-Django admin StackedInline in custom tab
- [Django]-Caught TypeError while rendering: 'BoundField' object is not iterable
4π
I struggled with this for a bit, trying to limit Bootstrap cards to 3 per card-deck.
This works in Django 2.1 and above, to limit a row to groups of 3:
{% ifequal forloop.counter|divisibleby:"3" True %}
π€Jorge
- [Django]-In Django, how to rename user model?
- [Django]-Django Rest Framework IsAuthenticated permission Error Anonymous user
- [Django]-Opening a new browser tab invalidates Django's CSRF token, preventing form submission
- [Django]-Django-summernote image upload
- [Django]-Bulk insert on multi-column unique constraint Django
- [Django]-Is there a simple way to create Chained Dynamic Drop Down List in Django Admin Site?
- [Django]-ModuleNotFoundError: No module named 'ebcli'
1π
You donβt want to do it like that β thatβs what cycle
is for.
{% for p in plist %}
<li{% ifnotequal forloop.counter 1 %}{% cycle ' class="clear"' '' '' '' %}{% endifnotequal %}>{{p.title}</li>
{% endfor %}
That example clears the 5th, 9th, 13th etc.
Edit: hat tip @cpharmston.
π€Dominic Rodger
- [Django]-How to download a file uploaded using django-filebrowser?
- [Django]-Django aggregate average of an annotation sum (1.6.5)
Source:stackexchange.com