One of the main reasons why we're using Bootstrap-Flask in this project is because it has one of the most convenient methods for generating forms with WTForms.
Literally, in 1 line of code, you can create your form. It's as simple as:
{{ render_form(form) }}
What this line of code will do is generate all the labels, inputs, buttons, styling for your form just by taking the WTForm object that was passed to the template (form
).
You can simply delete the entire <form>
element.
Then, add a line to import the render_form() function from bootstrap-flask.
Finally, use the render_form()
to generate your form
.
Run your code and see the entire form laid out for you with zero effort. Also, check out the error messages from validation!
Now you might wonder, why did I put you through all that hassle to learn how to create a WTForm from scratch when I knew all along that you can just use the Bootstrap-Flask render_form()
? Because everything is dandy as long as it works. This render_form macro is a black box. It's magic. Which is great, but what happens if your form breaks? What if it's not doing what you expect it to? How would you debug magic?
That's why it's so important to understand how things work under the hood. Once you understand it, you can take all the shortcuts.
You can download the completed project from this lesson's resources.