A
A
Aaltsin2019-04-25 14:32:03
Flask
Aaltsin, 2019-04-25 14:32:03

What is the best way to edit form data?

Suppose there are several forms for entering a mathematical formula and integration limits on the site. For python, the most well-known exponent '^' looks like '**', in addition, it is necessary to replace commas in real numbers with periods, remove extra spaces and convert everything to common case. In order not to force the user to adhere to all these nuances, it is necessary to edit the data on the server side.
I implemented it all in one function and for each form I use this function, it looks something like this: guard(request.form['foo']), guard(request.form['a']), etc.
Is this the right solution or is there a better one?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-04-25
@Aaltsin

In Flask, this task is best solved by creating your own field:

from wtforms import StringField

class FormulaField(StringField):
    def process_formdata(self, valuelist):
        if valuelist:
            self.data = [guard(i) for i in valuelist]
        else:
            self.data = []

And then use it in the appropriate forms:
from flask_wtf import FlaskForm

class MathForm(FlaskForm):
    formula = FormulaField('Формула', validators=[DataRequired()])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question