How to integrate the message form into the AnQiCMS template and dynamically display the custom form fields configured in the backend?

Calendar 👁️ 58

In today's digital world, websites are not just platforms for displaying information, but also important bridges for interaction and communication with users.A well-designed, feature-complete feedback form that can greatly enhance user experience, help websites collect feedback, and obtain business opportunities.AnQiCMS (AnQiCMS) understands this well, providing strong form integration capabilities, especially impressive is that it allows us to flexibly configure custom form fields through the backend and dynamically present these fields in front-end templates, thereby meeting various personalized business needs.

Understand the core advantages of the AnQi CMS message form

The Anqi CMS message function is not simply to provide a few fixed input boxes, but it gives website operators a great degree of freedom.Through the "website message management" feature on the backend, we can easily add, modify, or delete form fields.This means that whether it is product consultation, technical support, or cooperation negotiation, we can customize exclusive message forms for different scenarios without modifying a single line of code, which undoubtedly brings a qualitative leap to the flexibility and maintainability of the website.The ability to dynamically configure is exactly one of the highlights of Anqi CMS as an enterprise-level content management system.

Integrate the message form into the template

Integrating the message form into the website template is the first step in achieving this powerful function. According to the template design conventions of Anqi CMS, the message page usually corresponds to the template directory underguestbook/index.htmlorguestbook.htmlFile. Of course, you can also embed it into any page as needed, such as the product details page or the contact us page.

In the template file, we introduce the background configuration留言field through a concise template tag:

{% guestbook fields %}
    {# 在这里我们将动态生成表单字段 #}
{% endguestbook %}

here,guestbookThe tag encapsulates all the background configuration留言field information into a namedfields. ThisfieldsA variable is an array that contains detailed properties of each field, such as name, type, and whether it is required.Next, we can use this information to automatically build forms in the front-end template.

Build custom form fields dynamically

Dynamic display of custom fields is the essence of the Anqi CMS message form. We will iteratefieldsArray, and according to the type of each field (item.TypeThis method is used to render different HTML form elements. This ensures that the front-end form stays synchronized with any changes made to the fields in the backend, greatly reducing the manual effort to modify templates.

Generally, we would useforLoop combinationifconditional statements to handle different types of fields. Here is a practical template code snippet that shows how to dynamically generate common form elements:

<form method="post" action="/guestbook.html">
{% guestbook fields %}
    {% for item in fields %}
    <div class="form-group">
        <label for="{{item.FieldName}}">{{item.Name}}</label>
        <div>
            {% if item.Type == "text" or item.Type == "number" %}
            <input type="{{item.Type}}" name="{{item.FieldName}}" id="{{item.FieldName}}"
                   {% if item.Required %}required{% endif %}
                   placeholder="{{item.Content}}" autocomplete="off">
            {% elif item.Type == "textarea" %}
            <textarea name="{{item.FieldName}}" id="{{item.FieldName}}"
                      {% if item.Required %}required{% endif %}
                      placeholder="{{item.Content}}" rows="5"></textarea>
            {% elif item.Type == "radio" %}
            <div class="form-check-inline">
                {%- for val in item.Items %}
                <input type="{{item.Type}}" name="{{item.FieldName}}" id="{{item.FieldName}}-{{loop.index}}" value="{{val}}" class="form-check-input">
                <label for="{{item.FieldName}}-{{loop.index}}" class="form-check-label">{{val}}</label>
                {%- endfor %}
            </div>
            {% elif item.Type == "checkbox" %}
            <div class="form-check-inline">
                {%- for val in item.Items %}
                <input type="{{item.Type}}" name="{{item.FieldName}}[]" id="{{item.FieldName}}-{{loop.index}}" value="{{val}}" class="form-check-input">
                <label for="{{item.FieldName}}-{{loop.index}}" class="form-check-label">{{val}}</label>
                {%- endfor %}
            </div>
            {% elif item.Type == "select" %}
            <select name="{{item.FieldName}}" id="{{item.FieldName}}" {% if item.Required %}required{% endif %}>
                {%- for val in item.Items %}
                <option value="{{val}}">{{val}}</option>
                {%- endfor %}
            </select>
            {% endif %}
        </div>
    </div>
    {% endfor %}

    {# 引入验证码,如果后台开启 #}
    <div class="form-group" id="captcha-container" style="display: none;">
        <label for="captcha">验证码</label>
        <div style="display: flex;">
            <input type="hidden" name="captcha_id" id="captcha_id">
            <input type="text" name="captcha" id="captcha" required placeholder="请填写验证码" autocomplete="off" style="flex: 1;">
            <img src="" id="get-captcha" style="width: 120px; height: 40px; cursor: pointer; margin-left: 10px;" alt="验证码">
        </div>
    </div>

    <div class="form-group">
        <button type="submit" class="btn btn-primary">提交留言</button>
        <button type="reset" class="btn btn-secondary">重置</button>
    </div>
{% endguestbook %}
</form>

In the code above, we have customized the HTML structure for each field type (text/number/textarea/radio/checkbox/select). It is worth noting that:

  • item.Nameis used to display the friendly name of form fields.
  • item.FieldNameThis field is submitted as a form elementnameAttribute, the backend will receive data based on this name
  • item.RequiredIf it istrue, it will be automatically addedrequiredAttribute, prompts the user that this field is required.
  • For radio, checkbox, and dropdown lists,item.Itemsall possible values will be provided, we loop againitem.Itemsto generate each option.
  • item.Contentin text and number input boxes asplaceholder.

to submit the message form

The submission of the comment form is relatively direct,formlabel'smethodproperties of thepost,actionproperty points to/guestbook.html. When submitting, in addition to the above dynamically generated custom fields, Anqi CMS will also default to receiving several core fields:user_name(commenter's name),contact(Contact information),content(Message content). Even if your backend is not configured for these fields, you can manually add them to ensure the integrity of the data.In addition, if you need the backend to return data in JSON format instead of the default HTML response, you can add a hidden field to the form:<input type="hidden" name="return" value="json">.

Improve user experience: integrate captcha

To prevent malicious submissions and spam, website comments usually require captcha.The Anqi CMS backend provides the function to enable留言评论验证码.Once enabled in the background, you need to integrate the captcha display and refresh logic in the frontend template.This usually involves a captcha image and an input box.

We reserve in the above form code,

Related articles

How to display the comment list of articles in AnQiCMS template and support pagination?

Display article comment list and pagination elegantly in AnQiCMS template Article comments are an important part of website interaction, not only can they enhance user engagement, but also bring richer discussions and value to the website content.For operators, how to clearly and efficiently display these comments on the page and support pagination is the key to improving user experience.AnQiCMS provides a set of intuitive and powerful template tags, allowing you to easily achieve this goal.### Understand AnQiCMS template basics AnQiCMS uses similar

2025-11-07

How to implement the parameter filtering function of the article list in AnQiCMS (such as filtering by custom properties)?

As the content of the website becomes richer, users often hope to find the information they are interested in more accurately, rather than searching for a needle in a haystack of disordered content.For operators, providing flexible article list filtering functions can not only significantly improve user experience but also effectively help users discover more related content, thereby increasing the time spent on the website and interaction.AnQiCMS fully understands this need, through its highly flexible content model and powerful template tag system, making it extremely simple and intuitive to implement the parameter filtering function for the article list.Even if it is a custom attribute

2025-11-07

How to display Markdown formatted article content in AnQiCMS template and render it correctly to HTML?

In a content management system, Markdown has become the preferred writing format for many content creators, it is concise, efficient, and easy to convert to structured HTML.For AnQiCMS users, using Markdown to write articles can not only improve writing efficiency but also better organize content structure.Luckyly, AnQiCMS deeply supports Markdown formatted article content and provides a perfect mechanism to ensure that they are correctly rendered into beautiful HTML pages on the website front-end.### Start

2025-11-07

How does AnQiCMS template reference other template files (such as `header.html`, `footer.html`) to achieve code reuse?

In website construction and maintenance, how to efficiently manage code and avoid redundant labor is the key to improving development efficiency and ensuring website consistency.For users who build websites using AnQiCMS, its flexible template engine provides a powerful code reuse mechanism, allowing us to easily refer to other template files, such as the common header (header) and footer (footer), thereby achieving modular development.The template design of AnQiCMS borrows the syntax features of the Django template engine

2025-11-07

How to get and display the current year in AnQiCMS template?

When building and maintaining websites, we often need to display some dynamic information on the page, such as the current year.This is very useful for copyright statements, annual reports, or any scenario that requires real-time year information.AnQiCMS with its concise and efficient template system makes it easy to obtain and display the current year.AnQiCMS's template system borrows the syntax of the Django template engine, which means it provides intuitive tags and variables to manipulate content.

2025-11-07

How to implement multilingual site switch link display in AnQiCMS template and set hreflang?

Building multilingual websites in AnQiCMS and providing convenient language switching functions and correct SEO settings is a key step in expanding the international market and enhancing user experience.As a modern content management system developed based on the Go language, AnQiCMS provides strong and flexible support in this aspect, allowing content operators to easily manage global content publishing needs.

2025-11-07

How to dynamically fetch and display the cover thumbnail of an article or category in the AnQiCMS template?

In website operations, carefully designed thumbnails are crucial for attracting user clicks, enhancing page aesthetics, and optimizing search engine (SEO) performance.AnQiCMS as a powerful content management system fully considers this point, providing a flexible and convenient way to manage and call the cover thumbnails of articles, categories, and other content. ### AnQiCMS Thumbnail Mechanism Overview The AnQiCMS thumbnail mechanism is very intelligent and user-friendly.When publishing an article, the system allows you to manually upload a thumbnail.

2025-11-07

How to display any custom content of the background custom settings in the template of AnQiCMS (such as the custom help page URL)?

In website operation, we often encounter situations where we need to quickly adjust some information, such as updating the help page link, changing the status of a feature switch, or displaying some infrequently changed but flexible custom text on the page.If each modification requires delving into the code file, it will undoubtedly greatly reduce efficiency.AnQiCMS (AnQiCMS) fully understands this pain point and therefore provides a very convenient custom setting mechanism that allows you to easily configure any content in the background without writing code, and display it in the website template.### Backend Settings

2025-11-07