In website content display, we often encounter situations where we need to adjust the content layout or display logic based on certain characteristics of numbers.For example, we may need to insert an advertisement every few articles, or make the even and odd rows of the table display different background colors, or perform special operations when the list loops to a specific position.In AnQiCMS template system, based on Django template engine syntax, it provides a very practical filter that can easily meet this requirement.

This filter isdivisibleby。Its function is to determine whether a number (or a value that can be converted to a number) can be evenly divided by another number, and returns a boolean value (TrueorFalse)。This is a very direct and efficient solution for scenarios that need to perform modulo operations in templates.divisiblebyDetailed description of it.}

How to usedivisiblebyFilter

The method of use is very intuitive, usually{{ 待判断的数字 | divisibleby: 除数 }}. The result of this expression will beTrueorFalse. Therefore, it is often used with conditional judgment tags{% if ... %}to execute different template logic based on the judgment results.

Let us understand its practical application through several specific examples:

1. Simple integer division judgment

Suppose we want to directly determine if a fixed number can be evenly divided by another number.

{# 判断 21 是否能被 3 整除 #}
{% if 21|divisibleby:3 %}
    <p>21 可以被 3 整除。</p>
{% else %}
    <p>21 不能被 3 整除。</p>
{% endif %}

{# 判断 22 是否能被 3 整除 #}
{% if 22|divisibleby:3 %}
    <p>22 可以被 3 整除。</p>
{% else %}
    <p>22 不能被 3 整除。</p>
{% endif %}

Run the above code, it will output “21 can be divided by 3.” and “22 cannot be divided by 3.”, indicating that the filter accurately performs the judgment.

2. In the loop to implement interval display or style switching

In the list loop,divisiblebyFilter is related toforloop.Counter(current loop index, starting from 1) orforloop.Counter0(Current loop index, starting from 0) combined, can achieve very flexible layout control.For example, we can make the background color of every two articles different, or insert an advertisement spot every three articles.

{% for item in archives %} {# 假设 archives 是通过 archiveList 标签获取的文章列表 #}
    <div class="article-item {% if forloop.Counter|divisibleby:2 %}even-row{% else %}odd-row{% endif %}">
        <h3>{{ item.Title }}</h3>
        <p>{{ item.Description }}</p>
        
        {# 每隔 3 篇文章显示一个广告位 #}
        {% if forloop.Counter|divisibleby:3 %}
            <p class="ad-placeholder">这是每隔3篇文章出现的广告位。</p>
        {% endif %}
    </div>
{% endfor %}

In this example,forloop.Counter|divisibleby:2will return in each loop when the index is evenTrue, thus applyingeven-rowStyle, otherwise apply.odd-rowStyle. Also,forloop.Counter|divisibleby:3Then it controls the display frequency of the ad position.

3. Determine the divisibility of the dynamic number.

divisiblebyThe filter can not only handle fixed values but also be well applied to variables defined in templates.

{% set total_items = 10 %} {# 假设总共有 10 个项目 #}
{% set items_per_page = 3 %} {# 假设每页显示 3 个项目 #}

{% if total_items|divisibleby:items_per_page %}
    <p>总项目数 {{ total_items }} 可以被每页显示数量 {{ items_per_page }} 整除,页面正好分完。</p>
{% else %}
    <p>总项目数 {{ total_items }} 不能被每页显示数量 {{ items_per_page }} 整除,会有余数。</p>
{% endif %}

This example shows how to use variables asdivisiblebyThe input of the filter, which is used when handling pagination logic or statistics