In website operation, we often encounter situations where users need to enter 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) types 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) takes advantage of the powerful features of 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 in 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 a real number10Direct mathematical operations on strings often lead to errors, such as"10" + "5"It may become"105"instead of15.
The template filter of Anqi CMS is exactly designed to solve such problems.They allow us to directly transform and process variables at the template level without delving into the backend code, thereby converting these 'numeric strings' into usable numeric types or performing other forms of processing to ensure data accuracy and optimize display effects.
Core conversion filter: Convert string to number
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 tries to convert the input to an integer. If the conversion is successful, it will return the corresponding integer value;If the input cannot be parsed as an integer (for example, "abc" or "3.14"), it will return0.floatThe filter then converts the input to a floating-point number. AndintegerSimilarly, if the conversion fails, it will return0.0.
Usage example: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 these two filters, we can ensure that subsequent mathematical operations are performed on the correct numeric type.
addFilter: Flexible numerical addition
addThe filter is not limited to adding numbers, it can also intelligently handle mixed operations of numbers and strings. When using strings and numbersaddWhen, it will try to perform numeric addition. If one of the operands cannot be converted to a number, it will try to concatenate both as strings.This provides great flexibility in some scenarios, but it is usually recommended to use firstintegerorfloatclear conversion.
Usage example:
<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 based on the standard keyboard mapping, which is a very practical conversion for a specific scenario.
Usage example:
<p>电话号码转换:{{ "999-PONGO2"|phone2numeric }} {# 输出 999-766462 #}</p>
Strengthened processing: formatting, default values and logical judgments
In addition to direct type conversion, Anqi 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.
Usage example:
{% 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: Handle null values
The fields submitted from the front-end form may sometimes be empty or not filled in. These filters can provide a friendly default display for these empty values.
default: If the value of the variable is empty (including an empty string""If a value is provided as a default.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.
Usage example:
{% 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 number 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.
Usage example:
<p>从数字1234567890中获取倒数第三位:{{ 1234567890|get_digit:3 }} {# 输出 8 #}</p>