Campi del form tramite request.form
Recuperi i valori dai form HTML inviati.
Campi del form tramite request.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.
Forms Send Data in the Body
When an HTML form posts, the field values travel inside the request body, not the URL, so request.args will not find them.
Meet request.form
For posted form fields Flask gives you request.form, a dict-like object keyed by each input name on the page.
name = request.form["name"]The name Attribute Is the Key
A field shows up in request.form under its HTML name attribute, so name="email" becomes request.form["email"].
Use .get for Optional Fields
Just like with args, prefer .get so a field the user left out returns None instead of crashing your view with an error.
phone = request.form.get("phone")Provide a Default
Give .get a fallback and missing optional fields take a clean default, keeping the rest of your logic simple.
role = request.form.get("role", "user")Match the HTTP Method
Form data only arrives on a POST, so your route must allow POST or the browser gets a 405 before your code runs.
@app.route("/signup", methods=["POST"])Form Values Are Strings Too
Every form value is text. Convert ages, prices, or counts yourself, and guard against bad input that will not parse.
age = int(request.form.get("age", 0))Handle Multi-Value Fields
Checkboxes and multi-selects can send a key many times. Use getlist to collect every chosen value into a list.
picks = request.form.getlist("topics")Form vs Args at a Glance
Remember the split: request.args reads the URL query string, while request.form reads fields from the posted body.
values Merges Both Sources
Need either source without caring which? request.values combines args and form into one dict-like view for convenience.
token = request.values.get("token")A Simple Signup View
Putting it together, a signup handler reads name and email from the form and responds once it has them in hand.
@app.route("/signup", methods=["POST"])
def signup():
name = request.form["name"]
return f"Welcome {name}"Quick Check
Check your grip on reading form fields.
Recap: Form Fields
You pulled posted fields from request.form, used .get with defaults, handled multi-value inputs, and saw how it differs from args. 🙌
Domande Frequenti
La lezione «Campi del form tramite request.form» è gratuita?
Sì — il testo completo di «Campi del form tramite request.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 «Campi del form tramite request.form»?
Recuperi i valori dai form HTML inviati. 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 «Campi del form tramite request.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
- Query string tramite request.args
- Campi del form tramite request.form
- Body JSON tramite request.get_json
- Header, cookie e IP del client