Answer the question
In order to leave comments, you need to log in
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
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 = []
from flask_wtf import FlaskForm
class MathForm(FlaskForm):
formula = FormulaField('Формула', validators=[DataRequired()])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question