简体   繁体   English

在 Flask WTForms 提交中处理数据

[英]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.

相关问题 Flask WTForms 新输入不会在提交时覆盖预填充的数据 - Flask WTForms new inputs are not overwriting prepopulated data upon submission Flask-wtforms 重定向表单提交和传递变量 - Flask-wtforms Redirect on Form Submission and Pass Variables Flask和WTForms - 如何获取wtforms以刷新选择数据 - Flask and WTForms - how to get wtforms to refresh select data 将会话数据从Flask传递到WTForms - Passing in Session data from Flask to WTForms 在循环中发布Flask wtforms并保存数据 - posting Flask wtforms in loop and saving data 在flask-wtforms中将数据传递给多个forms - Passing data to multiple forms in flask-wtforms 如何为 Flask 应用程序制作自定义 WTforms 验证器,将表单限制为每个用户一次提交 - How can I make a custom WTforms validator for a Flask app that restricts form to one submission per user 如何在使用 Flask 和 WTForms 保留表单数据的同时重定向? - How to redirect while keeping form data using Flask and WTForms? 使用psycopg2使用数据库中的数据填充烧瓶中的表单(wtforms) - Populate form (wtforms) in flask with data from database using psycopg2 Flask wtforms 数据和 pic 文件更新不与 sqlite DB 同步 - Flask wtforms data and pic file update not syncing with sqlite DB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM