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

Calendar 👁️ 71

It is crucial to establish effective communication channels 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 and enhance the interactivity and stickiness of the website.For users using AnQiCMS, the system's built-in message feature provides a convenient and flexible solution, allowing you to easily display and submit message forms without writing complex code.

AnQi CMS message function overview

The Anqi CMS's message function is designed to be very user-friendly, allowing you to customize the fields of the message form in the background, including text, numbers, multi-line text, radio buttons, checkboxes, dropdown selections, and more types to meet the needs of different business scenarios.The system also supports captcha functionality, effectively preventing spam, and provides a comprehensive backend message management system for easy viewing, replying, and exporting user messages.

Part 1: How to display the message form on the website front-end

To display the message form on your Anqi CMS website, you first need to understand its template structure. Anqi CMS usually names the template file for the online message page asguestbook/index.htmlorguestbook.htmlThese files are located in the template theme folder you are currently using.

In these template files, you will find a file namedguestbookThe tag is a powerful tool provided by Anqi CMS, which can dynamically obtain all the message fields configured on the backend and pass them to the frontend template.Its basic usage is:{% guestbook fields %}...{% endguestbook %}.

Herefieldsis an array object that contains all the detailed information of each field you define in the background for the message form. EachitemInfieldsthe array has the following important properties:

  • item.NameThis is the field name displayed on the front end, such as "Your name", "Contact information".
  • item.FieldNameThis is the field corresponding to thenameattribute value, which is the key for backend field identification.
  • item.TypeIndicates the type of the field, for exampletext(Single-line text),number(Number),textarea(Multi-line text),radio(Single selection),checkbox(Select multiple) orselect(Dropdown select).
  • item.Required: A boolean value indicating whether the field is required.
  • item.Content: The default value or placeholder text for the field.
  • item.Items: If the field type isradio/checkboxorselectThis property will contain an array, listing all options.

Using this information, you can write aforLoop to dynamically generate a message form, thereby avoiding manual modification of the front-end code each time the fields are adjusted in the background. A typical dynamic form generation code snippet might look something like this:

<form method="post" action="/guestbook.html">
{% guestbook fields %}
    {% for item in fields %}
    <div>
        <label>{{item.Name}}</label>
        <div>
            {% if item.Type == "text" or item.Type == "number" %}
            <input type="{{item.Type}}" name="{{item.FieldName}}" {% if item.Required %}required{% endif %} placeholder="{{item.Content}}" autocomplete="off">
            {% elif item.Type == "textarea" %}
            <textarea name="{{item.FieldName}}" {% if item.Required %}required{% endif %} placeholder="{{item.Content}}" rows="5"></textarea>
            {% elif item.Type == "radio" %}
            {%- for val in item.Items %}
            <input type="{{item.Type}}" name="{{item.FieldName}}" value="{{val}}" title="{{val}}"> {{val}}
            {%- endfor %}
            {% elif item.Type == "checkbox" %}
            {%- for val in item.Items %}
            <input type="{{item.Type}}" name="{{item.FieldName}}[]" value="{{val}}" title="{{val}}"> {{val}}
            {%- endfor %}
            {% elif item.Type == "select" %}
            <select name="{{item.FieldName}}">
                {%- for val in item.Items %}
                <option value="{{val}}">{{val}}</option>
                {%- endfor %}
            </select>
            {% endif %}
        </div>
    </div>
    {% endfor %}
    <div>
        <div>
            <button type="submit">提交留言</button>
            <button type="reset">重置</button>
        </div>
    </div>
</form>

Of course, if you prefer to fully customize the HTML structure and style of the form, it is also possible. In this case, you just need to make sure that each form element'snameThe properties are defined in the backgroundFieldNameMaintain consistency. Anqi CMS will collect and process message data based on thesenameProperties. For example, the system will preset some core fields such asuser_name(Name of the person leaving a message),contact(Contact information) andcontent(留言内容),even if you do not explicitly define them in the background, you can use them directly on the front endnameproperties to build the form

Part two: how to submit comments

When the user fills out the message form and clicks submit, the form data needs to be sent to the Anqi CMS backend for processing. The submission of messages isactionfixed as/guestbook.html, and is usually usedPOSTMethod.

In addition to the aforementioneduser_name/contactandcontentcore fields, if you have customized other comment fields in the background, then thenameproperties of these fields also need to match the background definitionFieldNameMaintain consistency so that the backend can correctly receive and store data.

In addition, you can also control the response format returned by the backend by adding a hiddeninputfield:

<input type="hidden" name="return" value="html">

tovalueis set tohtmlIt will make the server return a complete HTML page as a response (usually a success or failure prompt page), while set tojsonIt will return a JSON formatted data, which is very useful when you need to submit a form asynchronously through AJAX.

The third part: Enhancing comment security - Adding captcha

It is very necessary to add a captcha function to the message form to prevent malicious submissions and spam comments.The Anqi CMS is built-in with captcha support, and the enabling process is divided into two steps: backend configuration and frontend integration.

1. Enable captcha on the backend:You need to find and enable the留言验证码function in the Anqi CMS backend management interface. This is usually configured in the "Background Settings" or "Feature Management" related menus.

2. Front-end integrated captcha:In the template file of the message form, you need to add the following HTML elements and JavaScript code:

  • A hiddeninputField used to store the unique identifier for the verification code (captcha_id)
  • A textinputField used for user input of the verification code (captcha)
  • One<img>A tag used to display the captcha image and bind a click event so that the captcha can be refreshed when the user clicks.
  • A JavaScript code that is responsible for sending/api/captchaThe interface initiates a request to obtain newcaptcha_idand the captcha image URL, and update it to the page.

The following is an example code snippet for an integrated front-end captcha (assuming you are using native JavaScript):

`twig

<input type="hidden" name="captcha_id" id

Related articles

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 perform simple number or string concatenation in AnqiCMS template?

In Anqi CMS template design, flexibly handling and displaying data is the key to building a dynamic website.This is a common need in daily development to concatenate numbers or strings.AnQi CMS provides various ways to achieve this goal, from simple direct combinations to powerful filters, which can meet your different content assembly scenarios.Understanding the Basics of Concatenation: Direct Output Equivalent to "Addition" In Anqi CMS template syntax, the most intuitive way to concatenate strings is to place multiple variables or text directly side by side. For example

2025-11-08

How to display the document comment list and implement pagination?

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 as the number of comments increases, it is necessary to properly handle the display and pagination of the comment list.The Anqi CMS provides very intuitive and powerful template tags, helping us easily achieve this goal.

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