Render and Submit a Form
Show fields in a template and handle submit.
Render and Submit a Form is a free Flask Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
From Class to Page
A form class is useless until a user can see it. Now you will render those fields into HTML and accept the submission back. 🖋️
Pass the Form to the Template
Create the form in your view and hand it to render_template. The template gets a live object full of fields it can draw.
form = LoginForm()
return render_template('login.html', form=form)Open the Form Tag
In Jinja, start with an HTML form tag set to method post. That tells the browser to send field values in the request body.
<form method="post">
...
</form>Render the Hidden Tag
Call form.hidden_tag() first inside the form. It drops in the CSRF token your post will need to be accepted.
<form method="post">
{{ form.hidden_tag() }}
</form>Draw a Field
Each field renders itself when you call it. Use field.label for the text and the field itself for the input element.
{{ form.username.label }}
{{ form.username() }}Pass HTML Attributes
Calling a field accepts keyword arguments that become HTML attributes. Add a class or placeholder right from the template.
{{ form.username(class="input", placeholder="Your name") }}Render the Submit Button
The submit field draws the button. Calling form.submit() produces the input that posts the whole form.
{{ form.submit() }}Allow POST in the Route
By default a route only answers GET. List methods so the same view can serve the page and receive the post.
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()Read Submitted Data
After a post, each field exposes a .data attribute holding the typed value, already coerced to the right Python type.
username = form.username.data
password = form.password.dataAuto-Populate on Reload
Flask-WTF fills fields from the request automatically. If you re-render after a post, the user's input stays in place.
form = LoginForm() # picks up posted data on POSTOne Template, Both Verbs
A single view handles GET and POST: build the form, process it on post, and re-render the same page otherwise.
form = LoginForm()
if form.is_submitted():
pass # handle data
return render_template('login.html', form=form)Quick Check
What must you render inside the form to include the CSRF token?
Recap
You rendered fields with labels and attributes, added hidden_tag for CSRF, allowed POST, and read values from each field's .data. 🎉
Frequently asked questions
Is the “Render and Submit a Form” lesson free?
Yes — the full text of “Render and Submit a Form” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.
What will I learn in “Render and Submit a Form”?
Show fields in a template and handle submit. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flask Academy?
No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Render and Submit a Form” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flask Academy lesson?
Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Define a FlaskForm Class
- Render and Submit a Form
- validate_on_submit and CSRF Tokens
- Custom Validators and Field Errors