Renderice y envíe un formulario
Muestre campos en una plantilla y gestione el envío.
Renderice y envíe un formulario es una lección gratuita de Flask Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Flask Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Flask Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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. 🎉
Preguntas frecuentes
¿La lección «Renderice y envíe un formulario» es gratis?
Sí — el texto completo de «Renderice y envíe un formulario» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Flask Academy, actualiza a CoddyKit PRO. El curso de Flask Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Renderice y envíe un formulario»?
Muestre campos en una plantilla y gestione el envío. Practicas Flask Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Flask Academy?
No se requiere experiencia previa. Flask Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.
¿Cuánto tiempo toma la lección «Renderice y envíe un formulario»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Flask Academy?
Sí. Cada lección de Flask Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Defina una clase FlaskForm
- Renderice y envíe un formulario
- validate_on_submit y tokens CSRF
- Validadores personalizados y errores de campo