2

I have a calendar generated by Django and styled with Bootstrap. Here is the code in the Django template :

<div class="content">
  {% for month in period.get_months %}
    <div class="col-md-3">
      <div class="row row-centered">
        <button class="btn btn-custom active" href="{% url "month_calendar" calendar.slug %}{% querystring_for_date month.start 2 %}">{{month.name}}</button>
      </div>
      <div>
        {% month_table calendar month "small" %}
      </div>
    </div>
  {% endfor %}
</div>

Now, since months have a different number of weeks they have different heights I would like to avoid something like this: enter image description here

I understand from this answer that the best solution would be to use a clearfix.

So, how can I modify the for loop in my template so that Django inserts an extra line <div class="clearfix"></div> every four items?

user2314737
  • 27,088
  • 20
  • 102
  • 114
  • 1
    Have you seen the [`divisibleby`](https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#divisibleby) filter? – Jens Astrup Jun 04 '17 at 20:28

1 Answers1

4

Django templates for loop store current index in forloop.counter variable. You can read about this in docs. So you can try to change you code like this:

<div class="content">
  {% for month in period.get_months %}
    <div class="col-md-3">
      <div class="row row-centered">
        <button class="btn btn-custom active" href="{% url "month_calendar" calendar.slug %}{% querystring_for_date month.start 2 %}">{{month.name}}</button>
      </div>
      <div>
        {% month_table calendar month "small" %}
      </div>
    </div>
    {% if forloop.counter|divisibleby:4 %}
        <div class="clearfix"></div>
    {% endif %}
  {% endfor %}
</div>
Dima Kudosh
  • 7,126
  • 4
  • 36
  • 46