How to display the document comment list and implement pagination?

Calendar 👁️ 73

When using AnQi CMS to manage website content, the comment function on the document detail page is an important part of interacting with readers.Effectively display these comments and ensure that the page maintains good loading speed and user experience when the number of comments increases, it is necessary to properly handle the display and pagination of the comment list.AnQi CMS provides very intuitive and powerful template tags, helping us easily achieve this goal.

Understanding the basics of displaying the comment list

To display comments on the document page, we mainly use the built-in Anqi CMScommentListThe tag is specifically designed to retrieve comment data related to a specific document.When you are on the detail page of a document, this tag will intelligently recognize the ID of the current document without the need to specify it manually, which is very convenient.

For example, in a typical document detail template, you can initially display comments in the following way:

{# 假设我们正在一个文档详情页,archive.Id会自动被标签识别 #}
{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
    {% for item in comments %}
    <div>
        {# 评论者的用户名,考虑到评论可能需要审核,这里增加了一个状态判断 #}
        <span>
            {% if item.Status != 1 %}
            审核中:{{item.UserName|truncatechars:6}}
            {% else %}
            {{item.UserName}}
            {% endif %}
        </span>
        {# 如果是回复,会显示被回复评论者的信息 #}
        {% if item.Parent %}
        <span>回复</span>
        <span>
            {% if item.Status != 1 %}
            审核中:{{item.Parent.UserName|truncatechars:6}}
            {% else %}
            {{item.Parent.UserName}}
            {% endif %}
        </span>
        {% endif %}
        {# 评论的发布时间,使用stampToDate过滤器进行格式化 #}
        <span>{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
    </div>
    <div>
        {# 显示评论内容,同样考虑审核状态 #}
        {% if item.Parent %}
        <blockquote>
            {% if item.Parent.Status != 1 %}
            该内容正在审核中:{{item.Parent.Content|truncatechars:9}}
            {% else %}
            {{item.Parent.Content|truncatechars:100}}
            {% endif %}
        </blockquote>
        {% endif %}
        {% if item.Status != 1 %}
            该内容正在审核中:{{item.Content|truncatechars:9}}
        {% else %}
        {{item.Content}}
        {% endif %}
      </div>
      {# 可以添加点赞、回复等交互功能,这里仅作示意 #}
      <div class="comment-control" data-id="{{item.Id}}" data-user="{{item.UserName}}">
        <a class="item" data-id="praise">赞(<span class="vote-count">{{item.VoteCount}}</span>)</a>
        <a class="item" data-id=reply>回复</a>
      </div>
    {% endfor %}
{% endcommentList %}

In the code above,commentListThe label assigns comment data tocommentsthe variable, and throughforWe loop through each comment. We useitem.UserNameTo get the commenter,item.ContentTo get the comment content,item.CreatedTimeTo get the publish time, and usestampToDateThe filter formats it into a readable date. It is worth noting that,item.StatusIt can be used to determine if a comment has passed the review, anditem.ParentIt is used to display the context of the reply, which is crucial for building a clear conversation flow.

Implement pagination for the comment list.

When there are many comments, loading all comments at once can cause the page to become too long, affecting user experience and page performance.At this point, we need to introduce the pagination feature. The pagination implementation of Anqi CMS is also very simple, just need tocommentListlabel'stypeAdjust the parameters and combine withpaginationTag it and it is.

First, tocommentListlabel'stypeparameters fromlistchanged topage, and setlimitParameters to control the number of comments displayed per page. For example, we set each page to display 10 comments:

{% commentList comments with archiveId=archive.Id type="page" limit="10" %}
    {# 评论列表的渲染方式与上方基本一致 #}
    {% for item in comments %}
    <div>
        {# ... 评论详情展示代码 ... #}
    </div>
    {% endfor %}
{% endcommentList %}

Next, incommentListBelow the label, use it immediatelypaginationtags to generate pagination navigation.paginationLabels will automatically identify the pagination context of the current page and generate corresponding page number links. You can specify how many page number buttons to display in the pagination bar according to your needs.showParameters to specify how many page number buttons to display in the pagination bar.

<div class="pagination">
    {% pagination pages with show="5" %}
    <ul>
        {# 首页链接 #}
        <li class="page-item {% if pages.FirstPage.IsCurrent %}active{% endif %}"><a href="{{pages.FirstPage.Link}}">{{pages.FirstPage.Name}}</a></li>
        {# 上一页链接 #}
        {% if pages.PrevPage %}
            <li class="page-item"><a href="{{pages.PrevPage.Link}}">{{pages.PrevPage.Name}}</a></li>
        {% endif %}
        {# 中间页码列表 #}
        {% for item in pages.Pages %}
            <li class="page-item {% if item.IsCurrent %}active{% endif %}"><a href="{{item.Link}}">{{item.Name}}</a></li>
        {% endfor %}
        {# 下一页链接 #}
        {% if pages.NextPage %}
            <li class="page-item"><a href="{{pages.NextPage.Link}}">{{pages.NextPage.Name}}</a></li>
        {% endif %}
        {# 尾页链接 #}
        <li class="page-item {% if pages.LastPage.IsCurrent %}active{% endif %}"><a href="{{pages.LastPage.Link}}">{{pages.LastPage.Name}}</a></li>
    </ul>
    {# 您还可以选择显示总评论数、总页数和当前页码等信息 #}
    <p>总数:{{pages.TotalItems}}条评论,总共:{{pages.TotalPages}}页,当前第{{pages.CurrentPage}}页。</p>
    {% endpagination %}
</div>

In this pagination code,pagesthe variable contains all information related to pagination, such as the current page number (pages.CurrentPage)、total number of pages (pages.TotalPages), first page link (pages.FirstPage.Link) and previous/next pages (pages.PrevPage.Link,pages.NextPage.Link) as well as the list of middle page numbers (pages.Pages.pages.Pageswe can generate specific page link, and throughitem.IsCurrentdetermine the current page, thus adding the corresponding style (such asactiveclass).

comment form integration

Although this article mainly focuses on the display and pagination of the comment list, the core of the comment function also cannot be separated from the submission of comments.AnQi CMS also provides a simple comment submission mechanism.A basic comment submission form would usually look like this:

<form method="post" action="/comment/publish">
  <input type="hidden" name="return" value="html"> {# 提交后希望返回html内容 #}
  <input type="hidden" name="archive_id" value="{% archiveDetail with name="Id" %}"> {# 隐藏字段,指定评论关联的文档ID #}
  <div>
    <label>用户名</label>
    <div>
      <input type="text" name="user_name" required placeholder="请填写您的昵称" autocomplete="off">
    </div>
  </div>
  <div>
    <label>评论内容</label>
    <div>
      <textarea name="content" placeholder="您的评论..." rows="5"></textarea>
    </div>
  </div>
  {# 如果后台开启了验证码,这里还需要添加验证码相关的输入框和图片 #}
  <div style="margin-top: 15px;">
    <button type="submit">提交评论</button>
    <button type="reset">重置</button>
  </div>
</form>

Please note,archive_idIt is a mandatory hidden field that tells the system which document this comment is for.If your website backend has enabled comment captcha, you also need to add captcha-related fields and images in the form. For specific implementation, please refer to the Anqi CMS documentation.

By following these steps, you can elegantly display the document comment list on the website built with Anqi CMS, and combine it with pagination features to provide readers with a smooth reading and interactive experience.


Frequently Asked Questions (FAQ)

1. Why is my comment list not displaying any comments, even though there is comment data on the backend?This is usually due to the review status of comments. In AnQi CMS, comments may need to be reviewed by an administrator before they can be displayed on the front page. Please check.commentListWithin the tag.item.StatusThe judgment logic ensures that you have displayedStatus = 1(Reviewed and approved) comments. If the background is set to require review for comments by default, then newly submitted comments may not be displayed on the front page immediately.

2. How can I set different pagination styles for the comment lists of different document models?The AnQi CMS template system supports high customization. You can use the document model ID in the template file (moduleId) in the template file.ifJudgment statements, then load different pagination styles or layouts. For example, you can checkarchive.ModuleIdThe value, then apply different CSS classes or refer to differentpartialPagination templates. In addition,design-director.mdIt mentions that the template supports custom names, for examplecomment/list.htmlYou can create specific comment templates for different model comment lists and associate them in the background.

How to display the comment list of a specific document on a non-document detail page?IncommentListTagged,archiveIdThe parameter can be manually specified. This means that even if you are not on the document details page, as long as you know the ID of the target document, you can{% commentList comments with archiveId="目标文档ID" type="page" limit="10" %}the way to get and display the comment list and pagination of the document.

Related articles

How to display the comment form and submit comments in Anqi CMS?

It is crucial to establish an effective communication channel with visitors in website operation, and the message function is one of the key tools to achieve this goal.It can not only help websites collect user feedback and answer questions, but also promote user participation, enhance the interactivity and stickiness of the website.For users using AnQiCMS, the built-in message function provides a convenient and flexible solution, allowing you to easily display and submit message forms without writing complex code.### Overview of AnQi CMS Message Function The AnQi CMS message function is designed to be very user-friendly

2025-11-08

How to debug and view the complete structure and value of variables in the template?

When developing website templates in AnQi CMS, we often need to understand the specific structure of a variable on the page and what data it contains so that we can accurately call its properties.When dealing with complex business logic or unfamiliar template variables, being able to quickly view the complete structure and value of variables is undoubtedly the key to improving development efficiency.The Anqi CMS based on the Django template engine provides a concise and powerful variable debugging method for us.### Core Tool: `dump` Filter An enterprise CMS template provides a `dump` named

2025-11-08

How to determine if a string or array contains specific content in AnqiCMS?

In the daily work of content operation, we often need to make judgments and displays based on specific information of the content.For example, if a blog post mentions a hot keyword, we may want to give it a special tag; or if a product description includes a certain feature, we want to adjust its display style accordingly.The template engine of AnQiCMS provides a flexible and intuitive way to determine whether a string or array contains specific content, making dynamic content display and personalized processing very convenient.

2025-11-08

How to convert an array to a string with a delimiter, or split a string into an array?

In AnQi CMS template development, we often encounter situations where we need to handle data format conversion, especially when combining multiple values into a string or splitting a string containing multiple values into independent data.This is especially common when dealing with tags, multi-select properties, or extracting information from custom fields. 幸运的是,AnQi CMS built-in very practical template filters, can help us easily achieve the conversion between arrays and string with delimiters.

2025-11-08

How to implement the like function for comments in AnQi CMS?

In website operation, enhancing user interaction is a key factor in maintaining the vitality of the website and increasing user stickiness.The comment section is an important battlefield for users to express their opinions and interact with each other. Adding a like feature would undoubtedly significantly enhance the interactivity and community atmosphere of the content.The Anqi CMS is an efficient, customizable, and feature-rich enterprise-level content management system that provides flexible mechanisms, allowing users to easily add the popular interactive feature of liking comments in the comment section.This article will give a detailed explanation of how to implement the like function for comments in Anqi CMS.

2025-11-08

How to directly output HTML code without being automatically escaped in AnQiCMS templates?

In AnQiCMS templates, we often need to flexibly control the way content is displayed.You may have encountered such a situation: you carefully formatted some content containing HTML tags in the rich text editor on the back end, but when the output was displayed on the front page, the tags were displayed as plain text, rather than rendered according to the HTML structure.This is actually a security mechanism that is in operation by default in AnQiCMS and many modern content management systems.

2025-11-08

How to safely display HTML content generated by the rich text editor in AnQiCMS templates?

In website operation, we often need to handle content from rich text editors, which usually contains richly formatted HTML code.However, when displaying these HTML contents on a website template, security is the primary consideration.If not properly handled, malicious code (such as XSS attacks) may be injected, thereby damaging the website and users.The AnQi CMS is a highly efficient, customizable, and secure enterprise-level content management system that has fully considered content security issues from the outset.

2025-11-08

What potential security risks should be considered when outputting HTML using the `safe` filter?

In AnQiCMS template development, the `safe` filter is a very useful tool that allows us to directly output some content containing HTML tags to the page, rather than escaping the HTML tags as well.This is crucial for displaying details of articles generated by rich text editors, custom HTML modules, and other scenarios, as it ensures the correct rendering of the style and structure of the content.However, like all powerful tools, the use of the `safe` filter also comes with some potential security risks that should not be overlooked.

2025-11-08