Renderizzare e inviare un form
Mostri i campi in un template e gestisca l'invio.
Renderizzare e inviare un form è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Flask Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Flask Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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. 🎉
Domande Frequenti
La lezione «Renderizzare e inviare un form» è gratuita?
Sì — il testo completo di «Renderizzare e inviare un form» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Flask Academy, passa a CoddyKit PRO. Il corso Flask Academy include 4 lezioni in totale.
Cosa imparerò in «Renderizzare e inviare un form»?
Mostri i campi in un template e gestisca l'invio. Eserciti Flask Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Flask Academy?
Non è richiesta alcuna esperienza precedente. Flask Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «Renderizzare e inviare un form»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Flask Academy?
Sì. Ogni lezione Flask Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Definire una classe FlaskForm
- Renderizzare e inviare un form
- validate_on_submit e token CSRF
- Validator personalizzati ed errori dei campi