In wtforms, how to use one form to set defaults for another

16 views Asked by At

This is my first attempt to use WTF. I would like to collect data from one form and use it to set a default in another. I am using a global to try to do this, because I don't know another way. Here is the code, but the global doesn't convey info from one form to the other. Obviously I have some deep misunderstanding that is getting in the way. Any advice would be welcome.

#GLOBALS
stIn = ''

class InputForm(FlaskForm):
    inputfile = StringField('Name of file to edit: ', \
    default = '', validators=[Length(0,40)])
    submit = SubmitField('Submit')
    btn_cancel=SubmitField(label='Cancel',render_kw={'formnovalidate': True})

class OtherForm(FlaskForm):
    stInfile = StringField('File to edit:',default = stIn, \
    validators=[Length(0, 60)])
    submit = SubmitField('Submit')
    btn_cancel = SubmitField(label='Cancel', render_kw={'formnovalidate': True})

@app.route('/', methods=['GET', 'POST'])
def get_input():
    global stIn
    message = ''
    input_form = InputForm()
    if input_form.btn_cancel.data:
        return redirect('https://lutemusic.org', code=302)
    if input_form.validate_on_submit():
        stIn = input_form.inputfile.data
        return redirect('/form', code = 302)
    return render_template('input.html',  form=input_form, message=message)

# all Flask routes below
@app.route('/form', methods=['GET', 'POST'])
def form():
    form = OtherForm()
#   form.stInfile.default = stIn
    message = ""
    if form.btn_cancel.data:
        return redirect('https://lutemusic.org', code=302)
    if form.validate_on_submit():
        return redirect('/', code = 302)
    return render_template('form.html',  form=form, message=message)

if __name__ == '__main__':
    app.run(debug=True)
0

There are 0 answers