divisibleby Filter

Number

Argument: n (required) – divisor

Documentation

Returns True if the value is divisible by n, and False otherwise.

Variable

age = 33

Template

{{ age|divisibleby:2 }} {{ age|divisibleby:3 }}

Result

False True

Commentary

This Django template filter is similar to mod / modulus. It is useful for creating columns when using Bootstrap or some similar library.

Example Code:

<div class="row">
  {% for item in items %}
    <div class="col-4">
      {{ item }}
    </div>
    {% if forloop.counter|divisibleby:3 %}
      {# Start new row #}
      </div>
      <div class="row">
    {% endif %}
  {% endfor %}
</div>

Did we get something wrong? Is there a use case for the divisibleby filter that we should add? Please let us know.

Send Feedback

Official Documentation
This page last updated on Oct. 30, 2022, 1:21 p.m. EST