Sometimes, we want to create multiple forms in a single page using Python Flask and WTForms.
In this article, we’ll look at how to create multiple forms in a single page using Python Flask and WTForms.
How to create multiple forms in a single page using Python Flask and WTForms?
To create multiple forms in a single page using Python Flask and WTForms, we can create form classes and then use them in our views.
For instance, we write
class Form1(Form):
name = StringField('name')
submit1 = SubmitField('submit')
class Form2(Form):
name = StringField('name')
submit2 = SubmitField('submit')
to create 2 form classes.
Then in our view file, we write
form1 = Form1()
form2 = Form2()
# ...
def view():
if form1.submit1.data and form1.validate():
# ...
if form2.submit2.data and form2.validate():
# ...
to create the form instances.
And then we get the data from each form from the submit field for each form.
And we call validate
to validate each form’s values.
Conclusion
To create multiple forms in a single page using Python Flask and WTForms, we can create form classes and then use them in our views.