A
A
aderes2018-09-17 17:00:29
Flask
aderes, 2018-09-17 17:00:29

Flask-wtf how to store a value in a field after submitting it?

there is a simple form (flask-wtf):

class Calck(FlaskForm):
    sum = StringField('Сумма (руб.)', validators=[DataRequired()])
    statement = RadioField('statement',
                           choices=[('value_1','Заявление 1'),
                                    ('value_2','Заявление 2')],
                                    default='value_1')
    submit = SubmitField('Рассчитать')

tell me, is it possible to make it so that after sending the form value and redirecting to the same page, the previously entered value was automatically substituted in the sum field? I know how to do it by writing the value to the database and calling it from there, but maybe there is a way without using the database?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aderes, 2018-09-18
@de-iure

Well, if anyone is interested, besides me, I solved it this way: when creating an instance of the form, an instance of the storage class is created, the attributes of which are written to the values ​​entered in the form fields, and then when sending the form values ​​and redirecting to the same page, the values ​​​​are taken from the storage ...

class Value_calc():
    def __init__(self):
        self.sum_data = ''
        self.statement_data = 'value_1'

from app.services.forms import Calck, Value_calc

@bp.route('/calc', methods=['GET', 'POST'])
def calc():
    form = Calck()
    data_form = Value_calc()                                 # экземпляр хранилища значений form
    calc_validate(form, data_form)
    return render_template('calc.html', form=form)

def calc_validate(form, data_form):
    if form.validate_on_submit():
        state_fee = form.gscalc_arbitr(form.sum.data, form.statement.data)
        flash(state_fee)
        data_form.sum_data = form.sum.data
        data_form.statement_data = form.statement.data
        return redirect(url_for('services.calc'))
    else:
        form.sum.data = data_form.sum_data
        form.statement.data = data_form.statement_data
    return form.sum.data, form.statement.data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question