0Pricing
Django Academy · Lezione

Gestire i metodi get e post

Instradare i verbi HTTP verso i metodi della classe

Gestire i metodi get e post è una lezione Django Academy gratuita su CoddyKit. Questa è la lezione 3 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 Django Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Django Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Verbs Become Methods

In a CBV, each HTTP verb maps to a method. A GET request runs your get method; a POST request runs your post method.

Subclass the Base View

For full control over verbs, subclass the base View class. You then define exactly the methods you want to support.

class ContactView(View):
    pass

Define a get Method

Add a get method to show the page, for example an empty contact form. It takes self and request and returns a response.

def get(self, request):
    return render(request, "contact.html")

Define a post Method

Add a post method to handle submitted data. Django routes the POST request here automatically.

def post(self, request):
    name = request.POST.get("name")
    return redirect("thanks")

No More method Checks

You no longer write if request.method == "POST". Django dispatches to the right method based on the verb for you.

dispatch Routes the Request

Behind the scenes, a method called dispatch inspects the verb and calls the matching method. You rarely touch it directly.

Unsupported Verbs Get 405

If a request uses a verb you did not define, Django returns 405 Method Not Allowed automatically. Safe by default.

The Classic GET-then-POST Flow

The usual pattern: get shows a form, the user submits, and post processes it. Two methods, one clean class.

Redirect After POST

After a successful post, return a redirect instead of rendering. This stops duplicate submissions on refresh.

Share State via self

Both methods live in the same class, so you can store shared values on self or call helper methods you define.

Wire It With as_view

Route the class the usual way with as_view(). Django builds the dispatcher and your get and post just work.

path("contact/", ContactView.as_view())

Quick Check

What happens on a POST to a CBV that only defines get?

Recap: get and post

You saw how CBVs map verbs to methods: get shows, post handles, dispatch routes, and undefined verbs return 405. No manual method checks. 🚦

Domande Frequenti

La lezione «Gestire i metodi get e post» è gratuita?

Sì — il testo completo di «Gestire i metodi get e 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 Django Academy, passa a CoddyKit PRO. Il corso Django Academy include 4 lezioni in totale.

Cosa imparerò in «Gestire i metodi get e post»?

Instradare i verbi HTTP verso i metodi della classe Eserciti Django 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 Django Academy?

Non è richiesta alcuna esperienza precedente. Django 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 3 di 4.

Quanto tempo richiede la lezione «Gestire i metodi get e 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 Django Academy?

Sì. Ogni lezione Django 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

  1. Dalle view basate su funzioni alle view basate su classi
  2. TemplateView e get_context_data
  3. Gestire i metodi get e post
  4. Mixin e as_view()
← Torna a Django Academy