Reputation: 3346
When i submit one form I will loose the data pre-populated in the another form. What i mean, to reproduce the problem, submit the form_1, then i will loose the selected data in the form_2. So, this is ignored when submit.
test = {'1':True}
form = F(**test)
You can test this code to check the issue:
from flask import Flask, render_template
from flask_bootstrap import Bootstrap
from flask_wtf import Form
from wtforms import SelectField, SubmitField, TextField, BooleanField
app = Flask(__name__)
app.config['DEBUG'] = True
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
Bootstrap(app)
if __name__ == '__main__':
app.run()
class UserCriteria(Form):
factor_1_criteria = SelectField('A', choices=[('1', 'Bad'), ('2', 'Mediocre'), ('3', 'Quite good'), ('4', 'Awesome')])
submit = SubmitField('Update')
def interests():
class F(Form):
pass
setattr(F, 'submit_interests', SubmitField('Submit'))
interests = [{"id": 1, "industry": 'banks'},{"id": 2, "industry": 'soccer'}]
for name in interests:
setattr(F, str(name['id']), BooleanField(name['industry']))
test = {'1':True}
form = F(**test)
return form
@app.route('/', methods=['GET', 'POST'])
def index():
form_1 = UserCriteria()
form_2 = interests()
return render_template("users/profile.html", form_1=form_1, form_2=form_2)
What I am expecting is keep the pre-populated data still available after the submission of the other form.
Upvotes: 4
Views: 556
Reputation: 3346
After some further investigation i found that the problem is the request.form.
If i remove the request.form before unpacking when the other form is submitted, then everything works as expected. I don't know if there is another more friendly way, but this works for me.
if request.form.getlist('submit_criteria'):
del request.form
Upvotes: 2