How to determine if a number can be evenly divided by another number in AnQiCMS templates?

Calendar 👁️ 71

In website content display, we often encounter situations where we need to adjust the content layout or display logic based on the characteristics of some 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 the AnQiCMS template system, based on the Django template engine syntax, it provides a very practical filter that can easily meet this requirement.

This filter isdivisibleby. Its purpose 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 where modular arithmetic judgment is needed in templates.You can find information about in the AnQiCMS template filter documentdivisiblebyDetailed description.

How to usedivisiblebyFilter

The usage 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 result.

Let us understand its practical application through a few specific examples:

1. Simple integer division judgment

Suppose we want to directly determine whether 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 %}

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

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

In the list loop,divisiblebythe filter meetsforloop.Counter(The current loop index, starting from 1) orforloop.Counter0(The 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 ad slot 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:2It will return when the loop index is evenTruethereby applyingeven-rowStyle, otherwise applyodd-rowStyle. Also,forloop.Counter|divisibleby:3It controls the display frequency of the ad space.

3. Determine the divisibility of dynamic values.

divisiblebyThe filter can not only handle fixed values but also apply well to variables defined in the template.

{% 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 to handle pagination logic or statistics

Related articles

What will the `get_digit` filter return when processing non-numeric strings?

In AnQiCMS template development, we often use various filters to process data, where the `get_digit` filter is a convenient tool for extracting specific digits from numbers.However, when we turn our attention to a more common but potentially misunderstood scenario - that is, when the `get_digit` filter encounters a **non-numeric string**, its behavior becomes less obvious.Let's review the performance of the `get_digit` filter when dealing with pure numbers.When the data is of a standard numeric type

2025-11-08

How to extract a number at a specified position from a numeric string (e.g., extract the batch number from the product code)?

In the daily operation of websites, we often encounter situations where we need to handle various encodings or IDs, such as product codes, order numbers, batch numbers, etc., which often contain multiple segments of information, and we may only need to extract a part of the numbers.For example, from the complex product code “PROD20230815BATCH007”, we only want to quickly obtain the batch number “007”.The Anqi CMS, with its flexible content model and powerful template tag system, can easily meet such demands.Next, we will discuss how to use Anqi CMS

2025-11-08

What type of result will be obtained when the `add` filter processes addition of different data types?

In AnQi CMS template development, we often encounter scenarios where we need to process and transform data.(Filter) is a powerful feature that was born for this purpose, which can help us format, modify, or calculate variables with concise syntax.Today, let's delve deeply into one very practical filter: `add`, especially its specific performance in handling the addition of different data types.### `add` filter: Simplifies the combination of numbers and text The core function of the `add` filter, as the name implies, is to add two values together

2025-11-08

How to perform addition or concatenation operations between numbers and strings in AnQiCMS templates?

During the process of building a website, we often encounter the need to perform arithmetic operations on numbers in templates, or to concatenate different string content to form new text.For AnQiCMS users, understanding how to efficiently implement these operations in the template engine is the key to enhancing the flexibility of content display and development efficiency.AnQiCMS is a powerful template engine, drawing inspiration from Django's design philosophy, providing intuitive and feature-rich tags and filters to meet these needs.#### AnQiCMS Template Basic

2025-11-08

Can the `divisibleby` filter be used to implement alternating row colors or other conditional styles in a loop?

In the daily operation of website content, how to make the list data more readable and visually attractive is a key factor in improving user experience.AnQiCMS (AnQiCMS) offers a flexible template engine, providing rich possibilities for content display.Today, let's talk about a very practical template filter——`divisibleby`, and see how it helps us achieve alternate row coloring or other conditional styles in loops.## Get to know the `divisibleby` filter AnQi CMS template system

2025-11-08

How to format a Unix timestamp into a readable date and time string?

In website content management, the way time is presented is crucial to user experience.Although the system may prefer a unified and efficient Unix timestamp format for background data processing, for visitors, a string of random numbers is obviously not as intuitive and easy to understand as AnQi CMS knows this and provides a simple and powerful tool to solve this problem.### Unix timestamp: The 'time language' in the database Unix timestamp, in short

2025-11-08

What are the similarities and differences between the `stampToDate` and `date` filters in handling time formatting and their applicable scenarios?

In Anqi CMS template development, we often need to display time data in a user-friendly format.The system provides two very practical tools for handling time: the `stampToDate` function and the `date` filter.Although they can all help us format time, there are some key similarities and differences as well as applicable scenarios, understanding these can make our template development more efficient and accurate.## `stampToDate`: A timestamp handler In AnQi CMS

2025-11-08

How to truncate a long string and automatically add an ellipsis (...)?

In website operation, we often encounter situations where we need to display a piece of text, but we cannot let it be too long to avoid affecting the page layout or reading experience.Whether it is the title, abstract, or product description of an article, if the content exceeds the expected length, the usual practice is to truncate a part of it and add an ellipsis at the end to indicate that the content has not yet ended.For AnQiCMS users, achieving such an effect is not complicated, thanks to its flexible and powerful template engine, we have a variety of built-in filters (Filters) that can easily handle it

2025-11-08