Ask Your Question
2

How can three columns per row be looped using a for loop in a Django template?

asked 2023-06-11 22:14:58 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-06-11 22:22:01 +0000

plato gravatar image

Assuming you have a list of items you want to display in three columns, you can achieve this by looping through the list with a for loop and using the slice filter to split the list into groups of three. Here's an example:

{% for group in items|slice:"3" %}
  <div class="row">
    {% for item in group %}
      <div class="col-md-4">
        {{ item }}
      </div>
    {% endfor %}
  </div>
{% endfor %}

In this example, items is the list of items you want to display, and we use the slice filter to split it into groups of three. Then we loop through each group and create a new row for each one, with three columns inside for each item. The col-md-4 class ensures that each column takes up 1/3 of the available width.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-11 22:14:58 +0000

Seen: 19 times

Last updated: Jun 11 '23