How to build and display a custom field comment form on the front-end page?

Calendar 👁️ 83

When you want website visitors to be able to interact with you, such as submitting questions, providing feedback, or making appointments, a well-functioning contact form is essential.AnQiCMS provides a very flexible and powerful message management function, the core of which is the support for custom message fields, allowing you to build personalized forms according to your actual business needs.This article will guide you on how to build and display these comment forms with custom fields on the front-end page.

Why do you need a custom message form?

The traditional feedback form may only include basic fields such as name, email, and message content, but in many cases, these fields are far from meeting our needs.For example, if you run a training institution, you may need to collect students' ages, courses of interest;If you are a recruitment website, you may need to provide the educational background and work experience of job seekers.By custom fields, you can accurately collect the necessary information based on the specific functions of the website and user group, greatly enhancing the efficiency and user experience of data collection.The AnQiCMS custom message field feature was born for this, allowing you to achieve these personalized needs without writing code and through backend configuration.

Configure custom message field in the background

Firstly, we need to define the fields required for the留言表单留言表单in the AnQiCMS backend.

Log in to your AnQiCMS backend, navigate to the "Function Management" area of the left menu, and then click "Website Messages".Here, you can see the current list of comment fields.The system usually presets some basic fields, and you can also add or modify fields according to your needs.

Click Add New Field, you will see the following key configuration items:

  • Parameter name and calling field:
    • Parameter NameThis is the Chinese name of the field, which will be displayed to the user, such as 'Your name', 'Phone number'.
    • Field invocationIt is the English name used internally by the system for identification and storage of data, it is recommended to use concise lowercase letters, such asyour_name/phone.
  • Field typeThe AnQiCMS provides various field types to adapt to different data input needs, which is similar to the concept of custom fields in the content model:
    • Single-line text:Applicable to names, titles, short sentences, etc., for brief text entries.
    • Number:Only numbers are allowed, suitable for phone numbers, quantities, etc.
    • Multi-line text:Used for longer messages or detailed descriptions.
    • Single choice: Displayed as a radio button, the user can only select one option.
    • Multiple selections: Displayed as checkboxes, the user can select multiple options.
    • Drop-down selection: It is presented in a dropdown menu format, where users select an option from the predefined choices.
  • Mandatory?: Checking this option will force users to fill in this field, ensuring you collect critical information.
  • Default value/options:
    • For single-line text, numbers, and multi-line text, you can set the default fill content or prompt text here.
    • For single choice, multiple choice, and dropdown selection, you can enter your options one per line here, for example:
      
      男
      女
      保密
      
      The system will automatically generate the front-end options based on the options you enter.

After completing the field configuration, save the changes. These custom fields are already ready in the background, waiting to be called in the front-end page.

Build the comment form on the front-end page.

After the field configuration on the back-end is completed, the next step is to build and display this comment form on the website front-end.The AnQiCMS template system is based on the Django template engine syntax in Go language, providing strong flexibility, allowing you to easily render the fields defined in the backend into HTML forms.

First, you need to find or create a template file for displaying the comment form. According to the template conventions of AnQiCMS, the template file for the comment page is usually located in your template directory.guestbook/index.htmlorguestbook.htmlFor example, if your template name isdefaultthe file path might be/template/default/guestbook/index.html.

You can use it in the template file,{% guestbook fields %}The tag loads all comment fields configured on the backend. This tag stores all field information in a name calledfieldsIn the array object, you can iterate over this array to dynamically generate form elements.

Here is a detailed example of a front-end form construction that covers rendering of various field types and the logic of form submission:

`twig

{# 用于指定后端返回格式,html 返回页面,json 返回 JSON 数据,默认是 html #}
<input type="hidden" name="return" value="html">

{% guestbook fields %}
    {% for item in fields %}
    <div class="form-group">
        <label for="{{ item.FieldName }}">
            {{ item.Name }}
            {% if item.Required %}
                <span class="required-star">*</span> {# 必填字段的提示 #}
            {% endif %}
        </label>
        <div>
            {# 根据字段类型渲染不同的表单元素 #}
            {% if item.Type == "text" or item.Type == "number" %}
                <input type="{{ item.Type }}"
                       id="{{ item.FieldName }}"
                       name="{{ item.FieldName }}"
                       {% if item.Required %}required{% endif %}
                       placeholder="{{ item.Content }}"
                       autocomplete="off"
                       class="form-control">
            {% elif item.Type == "textarea" %}
                <textarea id="{{ item.FieldName }}"
                          name="{{ item.FieldName }}"
                          {% if item.Required %}required{% endif %}
                          placeholder="{{ item.Content }}"
                          rows="5"
                          class="form-control"></textarea>
            {% elif item.Type == "radio" %}
                {%- for val in item.Items %}
                <label class="radio-inline">
                    <input type="radio"
                           name="{{ item.FieldName }}"
                           value="{{ val }}"
                           {% if item.Required and forloop.First %}required{% endif %} {# 确保第一个选项是必选 #}
                           class="form-check-input">
                    {{ val }}
                </label>
                {%- endfor %}
            {% elif item.Type == "checkbox" %}
                {%- for val in item.Items %}
                <label class="checkbox-inline">
                    <input type="checkbox"
                           name="{{ item.FieldName }}[]" {# 多选框需要加 [] #}
                           value="{{ val }}"
                           {%

Related articles

How to display the list of user comments on articles in AnQi CMS?

In AnQi CMS, allowing users to comment on articles and clearly displaying these comments is an important aspect of enhancing the interactivity and user engagement of the website.AnQiCMS's powerful template engine and built-in features make this process flexible and efficient.This article will introduce in detail how to integrate the comment list and its related functions on the article detail page.

2025-11-08

How does Anqi CMS display and manage website friend links?

In website operation, friendship links play an important role. They not only help to improve the website's search engine ranking (SEO), but also bring valuable traffic, and are also a manifestation of cooperation and mutual trust between websites.Anqi CMS is well-versed in this, providing website operators with a直观 and efficient buddy link management and display solution, allowing you to easily master this feature.Next, we will delve into how to manage friend links in the background, to how to flexibly display them on the website frontend, and provide you with a detailed explanation of the friend links feature of Anqi CMS.

2025-11-08

How to display a list of recommended content related to the current article?

In content operation, guiding users to discover more interesting content is one of the key strategies to enhance user experience and extend the time spent on the website.AnQiCMS (AnQiCMS) provides very flexible and powerful features, helping us easily achieve the goal of displaying a list of recommended content related to the current article.Why is it necessary to have relevant recommended content?

2025-11-08

How to display the link to the previous and next article on the Anqi CMS content page?

In AnQi CMS, it is a common need to add navigation links for "Previous Article" and "Next Article" to enhance the user experience of the website and the efficiency of internal links.These links not only help visitors browse related content more smoothly, but also have a positive impact on the website's SEO performance.AnQi CMS provides a set of intuitive template tags that allow you to easily display these navigation features on the content detail page.### Core Function: Use `prevArchive` and `nextArchive` tags Anqi CMS passes through

2025-11-08

How to display system-level configurations such as the website logo, filing number, copyright information, etc.

The brand identity, legal statements, and core contact information, such as Logo, filing number, and copyright information, are an indispensable part of the professional image of a website.They not only convey trust and standardization to visitors, but are also important aspects of search engine optimization and legal compliance.In Anqi CMS, managing and displaying these system-level configurations becomes extremely intuitive and efficient. ### Backend Settings: The "Control Center" of Website Information AnQi CMS concentrates these core website information in the "Global Settings" of the backend, making management clear at a glance.

2025-11-08

How to embed and display mathematical formulas or flowcharts from the Markdown editor in the content?

In modern content operation, the professionalism and readability of the content are equally important.Especially when our website involves fields such as science, education, engineering, and so on, how to clearly and beautifully present complex mathematical formulas or logically rigorous flowcharts in articles often becomes the key to improving user experience.AnQi CMS understands this need, integrating a powerful Markdown editor and combining it with simple frontend configuration, allowing your website to easily present these professional contents.### First Step

2025-11-08

How does Anqi CMS handle images in content to optimize loading speed and display quality (WebP, compression)?

In the content of the website, images play a crucial role, they can enrich the visual effects and convey complex information.However, unoptimized images are often the culprit for slow website loading and poor user experience.As content operators, we fully understand the value of image optimization in improving website performance and user retention.AnQiCMS provides a series of powerful and intelligent functions in image processing, aiming to help us easily achieve a balance between image loading speed and display quality.

2025-11-08

How to retrieve and display custom parameters of content models such as articles, products, etc. in a template?

In Anqi CMS, when managing website content, we often encounter the need to display specific information in addition to standard fields such as title and text.For example, when operating an e-commerce website, in addition to the name and description of the product, it may also be necessary to display 'material', 'origin', 'warranty period', and so on;Or you may be running an information blog, where articles may need to display 'author source', 'publisher' and other information.The flexible content model design of AnQi CMS is to meet this diverse content display requirement.Today, let's delve deeper into the topic

2025-11-08