0Pricing
Django Academy · درس

كتابة Middleware مخصص

التعامل مع كل طلب واستجابة

كتابة Middleware مخصص درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Middleware in a Sentence

Middleware is a layer that wraps every request and response, letting you run code before a view and after it returns.

The Onion Model

Think of middleware as layers of an onion around your view. A request travels inward through each layer, then the response travels back out.

The Modern Style

A modern middleware is a callable that takes get_response once at startup and returns the handler Django calls per request.

A Minimal Middleware

The factory stores get_response, and the inner function runs your code around the call. This callable shape is all Django needs.

def simple(get_response):
    def handler(request):
        # before the view
        response = get_response(request)
        # after the view
        return response
    return handler

Code Before the View

Anything before get_response runs on the way in. It is perfect for logging, timing, or attaching data to the request.

def handler(request):
    request.start = time.time()
    return get_response(request)

Code After the View

Anything after get_response runs on the way out. Here you can read or modify the response before it reaches the browser.

response = get_response(request)
response["X-App"] = "demo"
return response

The Class-Based Form

You can also write middleware as a class with __init__ and __call__. It mirrors the function form but groups related hooks neatly.

class Timer:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        return self.get_response(request)

Register in MIDDLEWARE

Middleware only runs once you list its dotted path in the MIDDLEWARE setting inside settings.py.

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "myapp.middleware.simple",
]

Short-Circuiting a Request

If middleware returns a response itself instead of calling get_response, it short-circuits the chain and the view never runs.

One Job Per Middleware

Keep each middleware focused on a single concern, like timing or auth checks. Small layers are easier to order and debug.

Common Real Uses

Teams use custom middleware for request logging, adding headers, locale handling, and lightweight auth checks across the whole site.

Quick Check

Check how a modern middleware hands off to the next layer.

Recap: Building Middleware

You built custom middleware: a callable taking get_response, with code before and after the view, registered in the MIDDLEWARE setting. ✅

الأسئلة الشائعة

هل درس «كتابة Middleware مخصص» مجاني؟

نعم — نص درس «كتابة Middleware مخصص» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.

ماذا ستتعلم في «كتابة Middleware مخصص»؟

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

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

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

كم من الوقت يستغرق درس «كتابة Middleware مخصص»؟

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

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

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

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

  1. إشارات post_save وpre_delete
  2. ربط المستقبِلات بطريقة صحيحة
  3. كتابة Middleware مخصص
  4. ترتيب Middleware وprocess_view
← العودة إلى Django Academy