0

I've a template design like this

<div class="row">
      <div class="col-sm-8"></div>
      <div class="col-sm-4"></div>
</div>
<div class="row">
      <div class="col-sm-4"></div>
      <div class="col-sm-8"></div>
</div>

and I've tried using this article django template rows of multiple items

But output doesnt come as required. How can I do as required.

What I've tried:

{% for item in items %}
<div class="row">
   {% if forloop.counter|divisibleby:2 %}
       <div class="col-sm-4"></div>
       {% else %}
        <div class="col-sm-8"></div>
   {% endif %}
</div>
{% endfor %}
Community
  • 1
  • 1
Russell
  • 1,624
  • 5
  • 23
  • 43

2 Answers2

1

Your code produce one row per item. If I'm correct, you want 2 items per row.

{% with items_length = items|length%}
<div class="row">
{% for item in items %}
<div class="col-sm-{% cycle '4' '8'%}"></div>
{% if forloop.counter|divisibleby:2 and forloop.counter < items_length %}
</div>
<div class="row">
{% endif %}
{% endfor %}
</div>
{% endwith %}

Something like that should work.

TonyEight
  • 316
  • 3
  • 11
0

The following is one way to do it:

{% for item in items %}
<div class="row">
    {% if forloop.counter0|divisibleby:2 %}
        <div class="col-sm-8"></div>
        <div class="col-sm-4"></div>
    {% else %}
        <div class="col-sm-8"></div>
        <div class="col-sm-4"></div>
    {% endif %}
</div>
{% endfor %}

Notice the use of forloop.counter0.

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139