I have an array which prints the list of some elements. I want to print those elements in a group of say ‘4’. That is if our array has 10 elements. Then in my template first <div> shows first 4 elements and next <div> shows next 4 elements. and so on.
I have tried to print like as we prints in PHP but it does not work here so please suggest me some way to do that.
There are 9 products in c.list and i want to show them as i have mentioned above:
{% if c.list|length >= 1 or c.list|length < 5 %}
{% for p in c.list %}
<div class="dis_box1">
<div class="item_imagebox_01"><a href="/shop/product/{{p.title}}"><img style ="width:145px;height:190px;"alt="" src="{{ MEDIA_URL }}{{p.image}}"></a>
<div class="img_line1"></div>
</div>
<div class="left"><span class="heart_text1"><a href="/shop/product/jhgjgj/">{{p.title}}</a></span></div>
</div>
{% endfor %}
{% endif %}
This is the kind of work you should really be doing in your view.
In your view:
Then in your template:
I’m sure there’s a way to do this with itertools or some other fancy trick, but this is clean and easy to understand for beginners.