Creare un form HTML con POST
Crei un form la cui action punti a una route.
Creare un form HTML con POST è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 1 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.
Why Forms Matter
An HTML form is how a browser collects typed input and hands it to your server. It is the front door for almost every user action. 📝
The form Element
Everything starts with a form tag. The browser bundles whatever fields live inside it and sends them together when the user submits.
<form>
<!-- fields go here -->
</form>Point It With action
The action attribute names the URL that receives the data. Leave it empty to post back to the same page the form lives on.
<form action="/signup">Choose the method
Set method to post so the values travel in the request body, not the URL. That keeps form data tidy and out of browser history.
<form action="/signup" method="post">Name Every Field
Each input needs a name attribute. That name becomes the key Flask uses to find the value on the server side.
<input type="text" name="username">Add a Submit Button
A submit button triggers the send. Clicking it tells the browser to gather the named fields and fire the request to your action URL.
<button type="submit">Sign up</button>Match the Route Methods
The receiving route must allow POST. List it in methods, or Flask answers with a 405 and your form data never lands.
@app.route("/signup", methods=["GET", "POST"])
def signup():
...Read Values From request.form
On the server, posted fields live in request.form, a dict-like object keyed by each input name you defined in the HTML.
username = request.form["username"]Serve the Form on GET
When the request is a GET, just render the page that shows the empty form so users have something to fill out.
if request.method == "GET":
return render_template("signup.html")Handle the POST Branch
When the method is POST, read the fields and act on them. One view can both show the form and process the submission.
if request.method == "POST":
name = request.form["username"]A Complete Mini Form
Put it together: a form that posts to /signup and a view that greets the user. That is the full round trip in a few lines.
@app.route("/signup", methods=["GET", "POST"])
def signup():
if request.method == "POST":
return "Hi " + request.form["username"]
return render_template("signup.html")Quick Check
Let us confirm what makes a form actually post.
Recap: Posting Forms
You built a form with an action and post method, named your fields, allowed POST on the route, and read values from request.form. 🎉
Domande Frequenti
La lezione «Creare un form HTML con POST» è gratuita?
Sì — il testo completo di «Creare un form HTML con POST» è 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 «Creare un form HTML con POST»?
Crei un form la cui action punti a una route. 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 1 di 4.
Quanto tempo richiede la lezione «Creare un form HTML con POST»?
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
- Creare un form HTML con POST
- Leggere e impostare valori predefiniti per i parametri di query
- Validare manualmente i campi obbligatori
- Redirect dopo il POST (pattern PRG)