256👍
✅
You need divisibleby, a built-in django filter.
{% for p in posts %}
<div class="post width1 height2 column {% if forloop.counter0|divisibleby:4 %}first{% endif %}">
<div class="preview">
</div>
<div class="overlay">
</div>
<h2>p.title</h2>
</div>
{% endfor %}
19👍
Bootstrap rows and columns example.
New row every 4 items. Also close last row even if there are less than 4 items.
myapp/templatetags/my_tags.py
from django import template
register = template.Library()
@register.filter
def modulo(num, val):
return num % val
html template
{% load my_tags %}
{% for item in all_items %}
{% if forloop.counter|modulo:4 == 1 %}
<div class="row">
{% endif %}
<div class="col-sm-3">
{{ item }}
</div>
{% if forloop.last or forloop.counter|modulo:4 == 0 %}
</div>
{% endif %}
{% endfor %}
- [Django]-How can I check the size of a collection within a Django template?
- [Django]-Django-way for building a "News Feed" / "Status update" / "Activity Stream"
- [Django]-Inline in ModelForm
18👍
You can’t use the modulus operator in Django template tags, but it would be easy enough to write a filter to do so. Something like this should work:
@register.filter
def modulo(num, val):
return num % val
And then:
{% ifequal forloop.counter0|modulo:4 0 %}
You could even do something like this, instead:
@register.filter
def modulo(num, val):
return num % val == 0
And then:
{% if forloop.counter0|modulo:4 %}
Or you could use the cycle
tag:
<div class="post width1 height2 column {% cycle 'first' '' '' '' %}">
- [Django]-How to get Django and ReactJS to work together?
- [Django]-Django dump data for a single model?
- [Django]-Django testing: Test the initial value of a form field
- [Django]-How to completely uninstall a Django app?
- [Django]-Django proxy model and ForeignKey
- [Django]-How can i test for an empty queryset in Django?
Source:stackexchange.com