Diramare la logica con request.method
Serva un form con GET e lo elabori con POST.
Diramare la logica con request.method è 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.
One Route, Two Jobs
A route that accepts both GET and POST often needs to behave differently for each. You handle that by checking which verb arrived.
The request Object
Flask gives you a global request object that describes the incoming call. Import it once and use it inside any view.
from flask import requestRead request.method
The attribute request.method holds the verb of the current call as a string like "GET" or "POST".
if request.method == "POST":
print("handling a form")Branch on the Verb
Wrap your logic in an if that checks request.method. One branch shows a form, the other processes the submitted data.
The Classic Form Pattern
The most common shape is: on GET render the empty form, and on POST read the answers and act on them.
@app.route("/contact", methods=["GET", "POST"])
def contact():
if request.method == "POST":
return "saved"
return "show form"GET Should Not Change Data
Keep the GET branch read-only. A GET request must never create or modify records, since browsers may repeat it freely.
POST Does the Work
Put writes, saves, and side effects in the POST branch. That keeps mutations tied to a deliberate submission.
Compare Strings Exactly
Always compare against the exact uppercase value. request.method is "POST", never "post", so a lowercase check will silently fail.
An else for the Fallthrough
Treat the GET case as the natural else. Return the form view when the method is not POST so the page always renders.
Skip Checks Flask Handles
You never test for a verb you did not allow. Flask blocks unlisted methods before your code runs, so only listed verbs arrive.
Keep Branches Small
If each branch grows large, call helper functions from the if. The view stays a clean dispatcher between GET and POST.
Quick Check
Pick the branch that should change data.
Recap
Use request.method to branch: render the form on GET, process and save on POST. Keep GET read-only and POST for changes. Great work! 🙌
Domande Frequenti
La lezione «Diramare la logica con request.method» è gratuita?
Sì — il testo completo di «Diramare la logica con request.method» è 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 «Diramare la logica con request.method»?
Serva un form con GET e lo elabori con POST. 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 «Diramare la logica con request.method»?
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
- Dichiarare i metodi consentiti in una route
- Diramare la logica con request.method
- Leggere i dati POST dal body
- Spiegazione del 405 Method Not Allowed