0

I am trying to use django 1.10 templating system to display a list as a sequence of row, each containing at most 3 columns. In Jinja2 I would write:

{% for items in all_items|batch(3) %}
    <div class="row">
    {% for item in items %}
        <div class="col-md-4">
        <h1> {{item.name}} </h1>
        </div>
    {% endfor %}
    </div>
{% endfor %}

Using the batch filter in Jinja2 produces the desired output. I could not find an equivalent filter in django templates. What is the django way to achieve this ?

Jopela
  • 5,415
  • 2
  • 18
  • 19
  • Have you tried to use slice? https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#slice – Mehdi Pourfar Sep 25 '16 at 14:31
  • @MehdiPourfar it appears to me that slice will give me a subset of the all_items list, i'm looking to build a list of lists of size at most 3. I'm looking for the built-in way [to do this](http://stackoverflow.com/a/7374167/856645) – Jopela Sep 25 '16 at 14:36
  • There isn't a built in equivalent of bunch. You can either write a custom template filter, or use `forloop.counter` variable and `divisibleby` in the template. – Alasdair Sep 25 '16 at 14:39
  • @Alasdair I see. The answers to the similar question are fine for my use case. Thanks – Jopela Sep 25 '16 at 14:42

0 Answers0