รหัสสถานะและคลาสย่อยของ HttpResponse
ใช้ JsonResponse การเปลี่ยนเส้นทาง และตัวช่วย 404
รหัสสถานะและคลาสย่อยของ HttpResponse เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What Status Codes Mean
Every response carries a status code. The number tells the browser if things went well, moved, or broke. 🚦
The Friendly 2xx Codes
Codes in the 2xx range mean success. 200 is the everyday OK, and 201 signals that something new was created.
Redirects in the 3xx Range
A 3xx code tells the browser to go elsewhere. 301 is a permanent move, while 302 is a temporary redirect.
Client Errors: 4xx
The 4xx codes blame the request. 404 means not found, 403 means forbidden, and 400 means a bad request.
JsonResponse for APIs
Use JsonResponse to send data as JSON. Pass a dict and Django sets the right content type for you.
return JsonResponse({"name": "Ada"})Redirecting with redirect
The redirect shortcut returns a 302 to another URL or view name, so users land exactly where you want them.
return redirect('home')HttpResponseRedirect
Behind redirect sits HttpResponseRedirect. Give it a URL and it builds the 302 response for you directly.
return HttpResponseRedirect('/login/')Permanent Redirects
For a permanent move, use HttpResponsePermanentRedirect, which sends a 301 so browsers remember the new address.
Raising a 404
Raise Http404 when a record is missing. Django catches it and shows your not-found page with a 404 code.
raise Http404("No such post")get_object_or_404
The get_object_or_404 helper fetches a row or raises Http404 automatically, saving you a manual check.
post = get_object_or_404(Post, pk=3)Pick the Right Subclass
Choosing the right subclass sets a correct status with no manual numbers, keeping your views clear and honest.
Quick Check
Which response sends JSON with the correct content type?
Recap: Speak in Codes
Status codes and HttpResponse subclasses like JsonResponse, redirect, and Http404 let your views answer clearly. 🚀
คำถามที่พบบ่อย
บทเรียน “รหัสสถานะและคลาสย่อยของ HttpResponse” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “รหัสสถานะและคลาสย่อยของ HttpResponse” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “รหัสสถานะและคลาสย่อยของ HttpResponse”
ใช้ JsonResponse การเปลี่ยนเส้นทาง และตัวช่วย 404 คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “รหัสสถานะและคลาสย่อยของ HttpResponse” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม
ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- คำขอไปถึงวิวของคุณได้อย่างไร
- ออบเจ็กต์ HttpRequest
- ส่งคืน HttpResponse
- รหัสสถานะและคลาสย่อยของ HttpResponse