Sam
Sam

Reputation: 388

how to split a for loop in django

I want have a simple model for images. there is a option to upload unlimited images. but I want don't want them to be shown under each other. for instance just show the first 5 images then some text and then again another 5. right now my code is like this:

{% for img in images %}
 <img src="{{ img.images.url }}" class="img-fluid" alt="">
{% endfor %}

I want the Structure to be like this:

Thanks for the solutions and apologies for my bad English

Upvotes: 0

Views: 400

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476503

You can work with the |slice template filter [Django-doc]:

{% for img in images|slice:":5" %}
    <img src="{{ img.images.url }}" class="img-fluid" alt="">
{% endfor %}

…

{% for img in images|slice:"5:10" %}
    <img src="{{ img.images.url }}" class="img-fluid" alt="">
{% endfor %}

Upvotes: 3

Related Questions