0Pricing
Django Academy · 课时

返回 HttpResponse

构建响应并设置其状态和标头

返回 HttpResponse 是 CoddyKit 上的免费 Django Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Django Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Django Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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 HttpResponse

The 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 resp

Responses 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. 🎁

常见问题解答

「返回 HttpResponse」课时是免费的吗?

是的 — 「返回 HttpResponse」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Django Academy 课程的其余内容,请升级到 CoddyKit PRO。 Django Academy 课程共包含 4 节课。

「返回 HttpResponse」这节课中我会学到什么?

构建响应并设置其状态和标头 你通过在浏览器中直接运行的动手代码来练习 Django Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Django Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Django Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「返回 HttpResponse」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Django Academy 课中编写并运行代码吗?

能。每节 Django Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 请求如何到达您的视图
  2. HttpRequest 对象
  3. 返回 HttpResponse
  4. 状态码与 HttpResponse 子类
← 返回 Django Academy