0Pricing
Django Academy · درس

إرجاع HttpResponse

أنشئ استجابة واضبط حالتها وترويساتها

إرجاع HttpResponse درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 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» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.

ماذا ستتعلم في «إرجاع HttpResponse»؟

أنشئ استجابة واضبط حالتها وترويساتها تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟

لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «إرجاع HttpResponse»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟

نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. كيف يصل الطلب إلى View الخاصة بك
  2. كائن HttpRequest
  3. إرجاع HttpResponse
  4. رموز الحالة والفئات الفرعية لـ HttpResponse
← العودة إلى Django Academy