How to implement pagination in the content list?

Calendar 👁️ 68

When managing website content, especially when the amount of content increases, an intuitive and efficient pagination feature becomes particularly important.It not only allows visitors to find the information they need faster, improves the browsing experience, but also avoids performance issues caused by loading too much content on a single page.In AnQi CMS, implementing the pagination feature of the content list is flexible and direct, and can be configured with just a few simple steps.

AnQi CMS uses Django template engine syntax, allowing you to control the display logic of content through clear tags. To add pagination to your content list, we mainly use two core tags: one for retrieving the content list,archiveList(or similar content list tags), as well as specifically used for generating pagination navigationpagination.

Prepare your content list tags

Firstly, we need to determine which list needs pagination display in the template file. For the most common article list, you will usearchiveListTag to retrieve content from the database. The key is that you need to tellarchiveListThis is a list that requires pagination and specifies how many items to display per page.

You can use totypethe parameter to"page"and utilizedlimitParameters to define the number of items per page. For example, if you want to display 10 articles per page, you can set it like this:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
        <article>
            <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
            <p>{{item.Description}}</p>
            <time>{{stampToDate(item.CreatedTime, "2006-01-02")}}</time>
        </article>
    {% empty %}
        <p>抱歉,目前没有内容可以显示。</p>
    {% endfor %}
{% endarchiveList %}

In this code block,archiveListTags store content list data inarchivesvariables, forforLoop through and display.type="page"The settings will simultaneously generate a namedpagespagination object, which is the key to implementing pagination navigation next.

introducing pagination tags

With the data of the content list and the pagination object, you can add pagination navigation at the bottom of the page or in a suitable position. You need to passpaginationLabel, andarchiveListGeneratedpagesthe object to it.

paginationThe tag is very intelligent, it automatically generates the correct pagination links based on the current page number, total number of pages, and the URL structure of the content list tags. You can useshowThe parameter controls how many page number buttons are displayed in the pagination navigation, for exampleshow="5"It will display the 5 page numbers around the current page.

pagesThe object contains a series of useful properties, such asFirstPage(Home page),PrevPage(Previous page),Pages(Middle page number list),NextPage(Next page) andLastPage(End page). Each page number element (includingFirstPage/PrevPageetc.) hasName(Display Text),Link(link address) andIsCurrent(Is it the current page) properties, for flexible style control.

Here is an example of a basic pagination navigation:

<nav class="pagination-nav">
    {% pagination pages with show="5" %}
        {# 首页按钮,当处于首页时会应用active样式 #}
        <a href="{{pages.FirstPage.Link}}" class="page-link {% if pages.FirstPage.IsCurrent %}active{% endif %}">首页</a>

        {# 上一页按钮,只有当存在上一页时才显示 #}
        {% if pages.PrevPage %}
            <a href="{{pages.PrevPage.Link}}" class="page-link">上一页</a>
        {% endif %}

        {# 中间页码列表,遍历显示每个页码 #}
        {% for item in pages.Pages %}
            <a href="{{item.Link}}" class="page-link {% if item.IsCurrent %}active{% endif %}">{{item.Name}}</a>
        {% endfor %}

        {# 下一页按钮,只有当存在下一页时才显示 #}
        {% if pages.NextPage %}
            <a href="{{pages.NextPage.Link}}" class="page-link">下一页</a>
        {% endif %}

        {# 尾页按钮,当处于尾页时会应用active样式 #}
        <a href="{{pages.LastPage.Link}}" class="page-link {% if pages.LastPage.IsCurrent %}active{% endif %}">尾页</a>
        
        {# 您还可以显示总条数、总页数和当前页码 #}
        <span class="page-info">共 {{pages.TotalItems}} 条,{{pages.CurrentPage}}/{{pages.TotalPages}} 页</span>
    {% endpagination %}
</nav>

Integrated code snippet

Place the labels of the two parts in the same template file to achieve a complete pagination content list. Usually, the content list (archiveListThe part in the loop will be placed in the main area of the page, while the pagination navigation (paginationpart) will be placed below the content list.

For example, a typicallist.htmltemplate file structure might look like this:

{# 假设这是您的文章列表页面模板 #}
{% extends 'base.html' %} {# 继承基础模板 #}

{% block content %}
    <div class="articles-list">
        {# 内容列表区域 #}
        {% archiveList archives with type="page" limit="10" %}
            {% for item in archives %}
                <article class="article-item">
                    <h2><a href="{{item.Link}}">{{item.Title}}</a></h2>
                    <p class="article-description">{{item.Description}}</p>
                    <div class="article-meta">
                        <span>发布时间:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
                        <span>浏览量:{{item.Views}}</span>
                    </div>
                </article>
            {% empty %}
                <p class="no-content">抱歉,目前没有内容可以显示。</p>
            {% endfor %}
        {% endarchiveList %}
    </div>

    {# 分页导航区域 #}
    <div class="pagination-container">
        {% pagination pages with show="5" %}
            <nav class="pagination-nav">
                <a href="{{pages.FirstPage.Link}}" class="page-link {% if pages.FirstPage.IsCurrent %}active{% endif %}">首页</a>
                {% if pages.PrevPage %}
                    <a href="{{pages.PrevPage.Link}}" class="page-link">上一页</a>
                {% endif %}
                {% for item in pages.Pages %}
                    <a href="{{item.Link}}" class="page-link {% if item.IsCurrent %}active{% endif %}">{{item.Name}}</a>
                {% endfor %}
                {% if pages.NextPage %}
                    <a href="{{pages.NextPage.Link}}" class="page-link">下一页</a>
                {% endif %}
                <a href="{{pages.LastPage.Link}}" class="page-link {% if pages.LastPage.IsCurrent %}active{% endif %}">尾页</a>
            </nav>
            <div class="page-summary">
                当前第 {{pages.CurrentPage}} 页,共 {{pages.TotalPages}} 页,总计 {{pages.TotalItems}} 条内容。
            </div>
        {% endpagination %}
    </div>
{% endblock %}

{# 您还需要在样式文件中为 .pagination-nav 和 .page-link, .active 等类添加样式,以美化分页按钮 #}

By following these simple steps, you can secure in the company

Related articles

How to display the friend link list of the website?

During the operation of a website, friendship links are a common way for websites to recommend each other and share traffic. They not only help to increase the number of external links on the website, but also have a positive effect on SEO optimization, and can provide visitors with more valuable resources.AnQiCMS (AnQiCMS) provides a very convenient function for us to manage and display friend links. ### Managing Friend Links: Getting Started with Backend Operations Managing friend links in AnQi CMS is very intuitive.First, we need to log in to the website backend, find the "Function Management" item in the left function menu.

2025-11-09

How to add captcha functionality to a message or comment form?

During the operation of the website, the message and comment function is an important channel for enhancing user interaction and collecting feedback.However, the spam comments and malicious submissions that follow often bother website administrators.To effectively prevent these unexpected guests, adding a captcha function to the comment or message form is particularly important.AnQi CMS as a comprehensive content management system provides a convenient way to integrate this security mechanism, helping you maintain a clean, efficient interactive platform.### Preparation Before adding a captcha to your comment or message form

2025-11-09

How to build a custom website contact form?

The website feedback form is an important bridge for interaction between the website and visitors. Whether it is to collect user feedback, customer inquiries, or other interactive needs, a well-designed and functional feedback form is crucial.In Anqi CMS, building a custom message form is much simpler and flexible than you imagine.The built-in 'website message' function provides a powerful basic framework, allowing us to customize deeply according to actual needs. Next, we will discuss in detail how to build a custom message form in Anqi CMS that meets your specific needs.

2025-11-09

How to implement the like function for comments?

User interaction is an important manifestation of website vitality, which can significantly enhance the attractiveness and user stickiness of content.AnQi CMS is a powerful and highly customizable content management system that provides a rich set of features to enhance user engagement.Among them, the comment function is the core battlefield of user communication, and adding a like function to the comments can undoubtedly further stimulate community vitality and allow users to give more direct and positive feedback to high-quality comments.### Overview of AnQi CMS Comment Function AnQi CMS is built-in with a comprehensive comment management system, allowing website visitors to easily comment on articles

2025-11-09

How to customize parameter filtering for an article or product list?

In daily website operations, we often encounter such needs: we hope that users can quickly filter out the content they are interested in based on the different attributes of articles or products.For example, on a real estate website, users may want to filter for 'two bedrooms and one living room' 'school district' housing;On a product showcase website, users may want to find 'red' new T-shirts.AnQiCMS (AnQiCMS) provides a powerful and flexible custom parameter function, which can help us easily achieve this refined content filtering

2025-11-09

How to format a timestamp into a readable date in the template (such as: 2023-01-01)?

<ctrl94>thought Here's a thinking process that leads to the article above: 1. **Understand the Goal:** The user wants an article about formatting timestamps in AnQiCMS templates. I need to act as

2025-11-09

How to safely display HTML content retrieved from a database on the frontend without escaping?

In website operation, we often need to display some content with rich formatting, such as mixed text and images on article detail pages, HTML tables on product introduction pages, or interactive codes embedded in custom pages.This content is usually stored in a database and rendered on the front-end page.However, many content management systems (including the familiar AnQiCMS) by default, for website security, will escape the HTML content retrieved from the database, resulting in the front-end displaying the original HTML code rather than the expected effect.

2025-11-09

How to truncate long text content and add an ellipsis (...)

In the daily management of website content, we often encounter such situations: an article's summary, a product description, or a list item title, if the content is too long, it not only occupies too much page space but may also destroy the overall layout aesthetics.Especially on mobile and other small screen devices, overly long text can seriously affect user experience.How to elegantly truncate these texts and add common ellipses (...) to make the information complete and tidy, which is a very practical skill in content operation.

2025-11-09