← Back to Django Course | Chapter 12: Signals & Middleware | Lesson 1 of 8

Django Signals का परिचय

Signals एक bell की तरह हैं जो अपने आप बजती है ताकि आपके app के दूसरे हिस्सों को पता चले कि कुछ हुआ है, बिना आपको उन्हें सीधे call करना पड़े।
Syntax
markup
import django.dispatch

signal_name = django.dispatch.Signal()

signal_name.send(sender=SenderClass, key=value)

Django Signal क्या है?

एक signal Django का built-in publish/subscribe system है। एक piece of code एक signal send करता है जब कुछ होता है, और कई receiver functions कहीं और इसे listen कर सकते हैं और react कर सकते हैं, बिना sender को यह जानने की ज़रूरत के कि कौन सुन रहा है।

Note: Signals cross-app hooks के लिए बहुत अच्छे हैं, जैसे किसी भी app द्वारा User बनाए जाने पर एक welcome email भेजना।

उदाहरण: What Is a Django Signal?

A signal is Django's built-in publish/subscribe system. One piece of code sends a signal when something happens, and any number of receiver functions elsewhere can listen for it and react, without the sender needing to know who's listening.

markup
import django.dispatch

order_placed = django.dispatch.Signal()

def notify_shipping(sender, order_id, **kwargs):
    print(f"Notify shipping: order {order_id} placed")

order_placed.connect(notify_shipping)
order_placed.send(sender=None, order_id=42)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Signals बनाम Direct Function Calls

एक signal के लिए reach करने से पहले, पूछें कि क्या एक plain function call ज़्यादा simple होगा। एक direct call trace और debug करना आसान है; एक signal तब बेहतर है जब sender को genuinely यह नहीं जानना चाहिए कि कौन react करता है।

उदाहरण: Signals vs Direct Function Calls

Before reaching for a signal, ask whether a plain function call would be simpler. A direct call is easier to trace and debug; a signal is better when the sender genuinely shouldn't know who reacts to it.

markup
# Without signals: caller directly imports and calls
from shipping.utils import notify_shipping

def place_order(order_id):
    save_order(order_id)
    notify_shipping(order_id)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Built-in Signals का Overview

Django model lifecycle events के लिए कई ready-made signals ship करता है, जैसे django.db.models.signals से post_save, pre_save, और post_delete।

उदाहरण: Built-in Signals Overview

Django ships several ready-made signals for model lifecycle events, such as post_save, pre_save, and post_delete from django.db.models.signals.

markup
from django.db.models.signals import post_save, post_delete

def log_save(sender, instance, **kwargs):
    print(f"{sender.__name__} saved: {instance.pk}")

post_save.connect(log_save)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

@receiver से एक Receiver Connect करना

@receiver decorator एक function को एक signal से connect करने का सबसे common shorthand है, optionally sender= के साथ एक model तक scoped।

उदाहरण: Connecting a Receiver with @receiver

The @receiver decorator is the most common shorthand for connecting a function to a signal, optionally scoped to one model with sender=.

markup
from django.dispatch import receiver
from django.db.models.signals import post_save
from django.contrib.auth.models import User

@receiver(post_save, sender=User)
def welcome_new_user(sender, instance, created, **kwargs):
    if created:
        print(f"Welcome, {instance.username}!")
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मानना कि signals कई receivers के across एक guaranteed order में चलते हैं।
  2. Simple logic के लिए signals का overuse करना जिसे एक plain function call ज़्यादा clearly handle कर देता।
  3. Signals module import करना भूल जाना इसलिए इसके receivers असल में connect नहीं होते।
चैप्टर सारांश
  • Signals एक part of Django को दूसरे decoupled parts को notify करने देते हैं जब कुछ होता है।
  • django.dispatch.Signal आपको fully custom signals define और send करने देता है।
  • Receivers बस @receiver या .connect() से एक signal से connected functions हैं।
  • Signals cross-app decoupling के लिए convenient हैं लेकिन trace करने के लिए एक extra layer of indirection जोड़ते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.