In website operation, we often encounter situations where we need users to input numerical values in front-end forms, such as product quantity, price range, and user age, etc.However, these data obtained from the form, even though they look like numbers, are often treated as string (string) type by the Web system.This brings inconvenience to subsequent calculations, comparisons, and presentations, which may lead to type errors or inaccurate calculation results.AnQiCMS (AnQiCMS) boasts its powerful functionality based on the Django template engine, providing a series of concise and efficient filters that can elegantly handle such 'numeric string' conversion issues.

Understanding the role of front-end data and Anqi CMS filters

When a user fills in and submits a form on the front end, the browser will send the content of the input box as a text string to the server. For example, even if '10' is entered in the quantity input box, what is often received by the server is"10"such a string, not the actual number10Performing mathematical operations on strings directly often leads to errors, such as"10" + "5"might become"105"Instead15.

Core transformation filter: convert strings to numbers

The AnQi CMS provides a filter to directly convert strings to numeric types, which is the foundation for handling numeric values in front-end forms.

integerandfloatFilter

These filters are powerful tools for numeric type conversion.

  • integerThe filter attempts to convert the input to an integer.0.
  • floatThe filter converts the input to a floating-point number. Andintegersimilarly, if the conversion fails, it will return0.0.

Example Usage:suppose we get a variable namedquantity_inputfrom the form, whose value might be"10"or"hello".

{% set quantity_input = request.post.quantity|default:"0" %} {# 模拟从表单获取的字符串,并设置默认值 #}

{% set quantity_int = quantity_input|integer %} {# 将字符串转换为整数 #}
{% set price_float = request.post.price|float %} {# 将价格字符串转换为浮点数 #}

<p>转换后的整数数量:{{ quantity_int }}</p>
<p>转换后的浮点数价格:{{ price_float }}</p>

By passing through these filters, we can ensure that subsequent mathematical operations are performed on the correct number types.

addFilters: Flexible numeric addition

addThe filter is not limited to adding numbers, it can also intelligently handle mixed operations of numbers and strings. When used with strings and numbers,addWhen, it will try to perform numerical addition.If one of the operands cannot be converted to a number, it will attempt to concatenate both as strings.integerorfloatan explicit conversion is made.

Example Usage:

<p>数字相加:{{ 5|add:2 }} {# 输出 7 #}</p>
<p>数字与字符串相加(字符串可转为数字):{{ 5|add:"2" }} {# 输出 7 #}</p>
<p>数字与字符串相加(字符串不可转为数字):{{ 5|add:"个" }} {# 输出 "5个" #}</p>

phone2numericFilter: special conversion for phone numbers

For phone number strings containing letters,phone2numericthe filter can convert the letters to numbers according to the standard keyboard mapping, which is a very practical specific scenario conversion.

Example Usage:

<p>电话号码转换:{{ "999-PONGO2"|phone2numeric }} {# 输出 999-766462 #}</p>

Strengthened Processing: Formatting, Default Values and Logical Judgments

In addition to direct type conversion, Safe CMS also provides other filters to further optimize the processing and display of numbers.

floatformatFilter: Control floating-point precision

This filter allows us to precisely control the decimal places of floating-point numbers and perform rounding, which is very important for displaying values such as prices, percentages, etc.

Example Usage:

{% set amount = 34.23234 %}
<p>保留两位小数:{{ amount|floatformat:2 }} {# 输出 34.23 #}</p>
<p>保留三位小数:{{ amount|floatformat:3 }} {# 输出 34.232 #}</p>
<p>不保留小数:{{ amount|floatformat:0 }} {# 输出 34 #}</p>

defaultWithdefault_if_noneFilter: Handling null values

The fields submitted in the front-end form may sometimes be empty or not filled in. These two filters provide a friendly default display for these empty values.

  • default: If the value of the variable is empty (including an empty string)""),then use the provided default value.
  • default_if_none: If the value of the variable isnil(i.e., in Go language),nullor undefined), then use the provided default value. It will not handle empty strings.

Example Usage:

{% set user_age = request.post.age %} {# 假设用户未填写年龄 #}
{% set user_points = nil %} {# 假设用户积分未初始化 #}

<p>用户年龄:{{ user_age|default:"未填写" }}</p> {# 如果user_age为空字符串,输出“未填写” #}
<p>用户积分:{{ user_points|default_if_none:"无" }}</p> {# 如果user_points是nil,输出“无” #}

get_digitFilter: Extract the digits at the specified position

This filter can extract the specified position of a number in reverse order from a number, although it is not commonly used, it may be useful in some special data processing or display logic.

Example Usage:

<p>从数字1234567890中获取倒数第三位:{{ 1234567890|get_digit:3 }} {# 输出 8 #}</p>

divisible