0Pricing
Django Academy · درس

ربط المستقبِلات بطريقة صحيحة

تسجيل الإشارات في apps.py لتجنب المشكلات

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

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

Receivers Must Be Imported

A receiver only listens if Django actually imports the module it lives in. If the file is never loaded, the signal silently does nothing.

The Classic Trap

Putting receivers in models.py sometimes works by luck, and sometimes not. That fragility is the connection problem you want to avoid.

Use a signals.py File

The clean pattern is a dedicated signals.py inside your app that holds every receiver for that app in one obvious place.

from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=Profile)
def sync(sender, instance, **kwargs):
    pass

The AppConfig.ready Hook

Django calls ready() on each app config once at startup. That is the official place to load your signal handlers.

Import Inside ready

In apps.py, import your signals module from inside ready so receivers register exactly once when the app boots.

from django.apps import AppConfig

class BlogConfig(AppConfig):
    name = "blog"

    def ready(self):
        from . import signals

Point default_app_config

Make sure your app uses that config. Modern Django auto-discovers apps.py, so a matching AppConfig class is usually enough.

connect Without a Decorator

Instead of @receiver, you can wire a handler with connect. It does the same job and is handy for dynamic registration.

post_save.connect(sync, sender=Profile)

Avoiding Double Firing

If a module is imported twice, a receiver can run twice. Centralizing registration in ready() keeps each handler connected only once.

The dispatch_uid Safety Net

Pass a unique dispatch_uid so Django refuses to connect the same receiver more than once, even across reimports.

post_save.connect(sync, sender=Profile, dispatch_uid="profile_sync")

Disconnecting Receivers

You can also call disconnect to stop a handler, which is useful inside tests that should not trigger side effects.

Keep One App's Signals Together

Grouping each app's handlers in its own signals.py keeps reactions discoverable and stops them from scattering across the codebase.

Quick Check

Recall where signal handlers should be registered.

Recap: Wiring Signals Right

You learned to keep receivers in a signals.py, import them from AppConfig.ready(), and use dispatch_uid to prevent duplicate connections. ✅

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

هل درس «ربط المستقبِلات بطريقة صحيحة» مجاني؟

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

ماذا ستتعلم في «ربط المستقبِلات بطريقة صحيحة»؟

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

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

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

كم من الوقت يستغرق درس «ربط المستقبِلات بطريقة صحيحة»؟

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

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

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

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

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