Restituire un HttpResponse
Costruisca una risposta e ne imposti stato e intestazioni
Restituire un HttpResponse è 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.
Every View Returns One
A view must return an HttpResponse. Django sends that object to the browser, so returning it is not optional. 📤
from django.http import HttpResponseThe Simplest Response
Pass a string to HttpResponse and Django sends it as the page body. That string becomes what the browser shows.
def hi(request):
return HttpResponse("Hello!")Sending Real HTML
The body can be full HTML. The browser renders the tags, so you can return markup directly from a simple view.
return HttpResponse("<h1>Welcome</h1>")Setting the Status Code
Give status a number to control the result code. Use 201 for created or 403 for forbidden instead of the default 200.
return HttpResponse("Made it", status=201)The Content Type
The content_type tells the browser how to read the body. Switch it to text/plain to send raw text instead of HTML.
HttpResponse("hi", content_type="text/plain")Adding Response Headers
Treat the response like a dictionary to add headers. This is how you set things like caching or custom flags.
resp['X-App'] = 'CoddyKit'Setting Cookies
Call set_cookie on the response to store a value in the browser. It comes back on the next request automatically.
resp.set_cookie('theme', 'dark')Deleting a Cookie
Use delete_cookie to remove one you previously set. The browser drops it and stops sending it back to you.
resp.delete_cookie('theme')Build, Then Return
Create the response, customize it, then return it. You can set status, headers, and cookies before handing it back.
resp = HttpResponse("ok")
return respResponses Are Mutable
An HttpResponse is a normal Python object. You can adjust it across several lines before the view finally returns it.
It Travels Back Out
Once returned, your response goes back through middleware and out to the user, completing the cycle. ✅
Quick Check
How do you make a view respond with status 201?
Recap: You Control the Reply
An HttpResponse carries the body, status, content type, headers, and cookies. Shape it, then return it. 🎁
Domande Frequenti
La lezione «Restituire un HttpResponse» è gratuita?
Sì — il testo completo di «Restituire un HttpResponse» è 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 «Restituire un HttpResponse»?
Costruisca una risposta e ne imposti stato e intestazioni 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 «Restituire un HttpResponse»?
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
- Come una richiesta raggiunge la Sua view
- L'oggetto HttpRequest
- Restituire un HttpResponse
- Codici di stato e sottoclassi di HttpResponse