HttpResponseを返す
レスポンスを作成し、ステータスとヘッダーを設定します
「HttpResponseを返す」はCoddyKit上の無料Django Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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 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. 🎁
よくある質問
「HttpResponseを返す」レッスンは無料ですか?
はい。「HttpResponseを返す」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Django Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Django Academyコースには全4レッスンが含まれています。
「HttpResponseを返す」で何を学びますか?
レスポンスを作成し、ステータスとヘッダーを設定します ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Django Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDjango Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「HttpResponseを返す」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDjango Academyレッスンでコードを書いて実行できますか?
はい。すべてのDjango Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- リクエストがビューに届くまで
- HttpRequestオブジェクト
- HttpResponseを返す
- ステータスコードとHttpResponseのサブクラス