Returning an HttpResponse
Build a response and set its status and headers.
Returning an HttpResponse is a free Django Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. 🎁
Frequently asked questions
Is the “Returning an HttpResponse” lesson free?
Yes — the full text of “Returning an HttpResponse” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.
What will I learn in “Returning an HttpResponse”?
Build a response and set its status and headers. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Django Academy?
No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Returning an HttpResponse” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Django Academy lesson?
Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- How a Request Reaches Your View
- The HttpRequest Object
- Returning an HttpResponse
- Status Codes and HttpResponse Subclasses