Method one: Dynamically remove HTML tags from the template, leaving only plain text

If you want to keep the original content of the product description unchanged in the database but remove the HTML tags only on the frontend page, the template filter of Anqi CMS can easily achieve this.

The AnqiCMS uses Django template engine syntax, which provides.striptagsA filter that can remove all HTML tags from a segment of HTML content, leaving only plain text.

Application scenario:

  • Display product description summary on the product list page without complex formatting.
  • In a specific area of the website, output plain text content without any style.
  • I do not want to modify the original data in the database, keeping the HTML format for future use.

Operation Steps:

  1. Find the related template file:The product description is usually obtained througharchiveDetailorarchiveListTags, for example{{item.Description}}or{{item.Content}}You need to locate the template file that displays the product description (usuallydetail.htmlorlist.html). The specific path may vary depending on the template you are using.

  2. UsestriptagsFilter:Add after the product description variable where you want to display plain text|striptagsFilter. For example, if your product description is stored initem.Descriptionyou can modify it like this:

    <p>{{ item.Description|striptags }}</p>
    

    If your product description content is long, it can be stored initem.Contentand may contain more complex HTML structures, it can also be applied:

    <div>
        {% archiveDetail productContent with name="Content" %}
        {# 移除所有HTML标签,只保留纯文本 #}
        {{ productContent|striptags }}
        {% endarchiveDetail %}
    </div>
    

    Here, it is important to note,striptagsThe filter will remove all HTML tags, including images (<img>) and videos (<video>Tag. If you want images and videos not to be displayed, this is exactly what you need. If you need to retain some tags, for example, to remove only the script, you can consider usingremovetagsFilter and specify the tags to be removed.

  3. Save and view the effect:Save the modified template file, refresh the front-end page, and you will see that all HTML tags in the product description have been removed, leaving only concise plain text content.

The advantage of this method is non-destructive, it does not modify the original data in the database, and the template engine will dynamically process it each time it is rendered.

Method two: Use the background 'Site-wide Content Replacement' feature to batch permanently remove HTML tags

If you want to completely remove HTML tags from the product descriptions in the database and permanently convert the content to plain text, the 'Full Site Content Replacement' feature of Anqi CMS combined with regular expressions can achieve batch processing.

Application scenario:

  • There are a large number of historical data in the database, and the product descriptions all contain unnecessary HTML tags.
  • Subsequent content publishing will strictly use plain text format, hoping to unify data standards.
  • To reduce the size of the database or optimize certain API interface outputs.

Operation Steps:

  1. Enter "Document Keyword Replacement":Log in to the AQCMS backend management interface, find 'Content Management' in the left navigation bar, and then click 'Document Keyword Replacement'.This tool is named Keyword Replacement, but it supports powerful regular expressions and can be used to batch remove HTML tags.

  2. Configure replacement rules:

    • Select mode:In the replacement settings, select the 'Regular Expression' mode. This is a critical step because it allows you to use pattern matching to identify and remove HTML tags.
    • Replacement keyword:Enter a regular expression in the "Replace keyword" field to match all HTML tags. A commonly used and relatively safe regular expression is:
      
      <[^>]+>
      
      The meaning of this expression is: match all sequences that start with<Start, with>and do not contain>any character sequence. This can effectively capture most HTML tags.
    • Replace with:Leave the "Replace with" field blank. This means that all matched HTML tags will be removed and replaced with nothing, thus achieving the effect of removal.
    • Select field:In the 'Replace field' section, check the fields that include product descriptions. Usually, product descriptions are stored inContentorDescriptionField within. Please choose carefully to avoid misoperation of other fields.
    • Select model (if needed):If your website has multiple content models (such as article models, product models), and you only want to handle descriptions under the product model, you can select 'Product Model' in the filter conditions.

    (Image is for illustration purposes only, please enter the regular expression manually)

  3. Perform batch replacement (extremely important, please operate with caution!):Before clicking to perform the replacement,it is necessary to back up the database!This operation is irreversible. Once executed, the HTML tags in the product description will be permanently deleted from the database.After confirming that the backup is correct, click the "Execute Replacement" button.The system will iterate through all product descriptions that meet the criteria and remove the HTML tags.

    After the replacement, you can randomly check a few product descriptions to confirm that the HTML tags have been successfully removed.

Common Questions (FAQ)

Q1:striptagsandremovetagsWhat are the differences between filters? A1: striptagsThe filter will remove all HTML tags, regardless of type, as long as it is<>All package labels will be removed. AndremovetagsThe filter allows you to specify the HTML tags to be removed. For example,{{ item.Content|removetags:"script,style" }}will be removed<script>and<style>Tags, but keep other HTML tags. If you need to keep part of the format but remove specific functional tags,removetagswould be a better choice. If the goal is plain text,striptagsit is more efficient and direct.

Q2: What should you pay attention to before executing the background batch replacement operation, besides backing up the database? A2:Bulk replacement is a high-risk operation, and in addition to database backup, the following points should be noted:

  1. Carefully check the regular expression:Ensure that the regular expression you enter accurately matches HTML tags without affecting other content.&lt;[^&gt;]+&gt;is a commonly used expression, but it may not work for irregular or incomplete HTML.