[英]Process Data within Flask WTForms Submission
I would like to reformat the data submitted via WTForms by utilizing the process_data function .我想使用process_data function重新格式化通过 WTForms 提交的数据。 I am able to successfully submit the form, however, the data is not modified in the output as expected.我能够成功提交表单,但是,output 中的数据没有按预期进行修改。 I've included a simplified example of my forms.py file below.我在下面包含了我的 forms.py 文件的简化示例。
The goal of this form would be to input the value of "10,000" and return the string "10000".此表单的目标是输入值“10,000”并返回字符串“10000”。
from flask_wtf import FlaskForm
from wtforms import StringField
class SimpleForm(FlaskForm):
price = StringField('price')
def process_price(self, price):
price.data = str(price).replace(',','')
process_data
is a method on the base Field
class ( source ). process_data
是基于Field
class ( source ) 的一种方法。 To use, you need to write a custom implementation of some Field class ( StringField
, in your instance) and override this method, then use this field in your form definition.要使用,您需要编写一些字段 class(在您的实例中为StringField
)的自定义实现并覆盖此方法,然后在您的表单定义中使用此字段。 For example:例如:
from flask_wtf import FlaskForm
from wtforms import StringField
class CustomStringField(StringField):
def process_data(self, data):
self.data = str(data).replace(',', '')
class SimpleForm(FlaskForm):
price = CustomStringField('price')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.