ส่งคืน HttpResponse
สร้างการตอบกลับและกำหนดสถานะกับส่วนหัว
ส่งคืน HttpResponse เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ส่งคืน HttpResponse”
สร้างการตอบกลับและกำหนดสถานะกับส่วนหัว คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “ส่งคืน HttpResponse” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม
ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- คำขอไปถึงวิวของคุณได้อย่างไร
- ออบเจ็กต์ HttpRequest
- ส่งคืน HttpResponse
- รหัสสถานะและคลาสย่อยของ HttpResponse