0
0
Djangoframework~10 mins

Custom signals in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom signals
Define signal
Connect receiver function
Trigger signal.send()
Django calls receiver
Receiver executes custom code
Signal handling complete
This flow shows how a custom signal is defined, connected to a receiver, triggered, and handled in Django.
Execution Sample
Django
from django.dispatch import Signal, receiver

# Define signal
my_signal = Signal()

# Connect receiver
def my_receiver(sender, **kwargs):
    print(f"Received: {kwargs['data']}")

my_signal.connect(my_receiver)

# Trigger signal
my_signal.send(sender=None, data="Hello")
This code defines a custom signal, connects a receiver function, and triggers the signal with data.
Execution Table
StepActionEvaluationResult
1Define my_signalSignal object createdmy_signal ready to use
2Define my_receiverFunction definedmy_receiver ready
3Connect my_receiver to my_signalmy_signal.connect(my_receiver)Receiver registered
4Call my_signal.send(sender=None, data='Hello')Signal triggersmy_receiver called
5Inside my_receiver: print(f"Received: {kwargs['data']}")Print statement executedOutput: Received: Hello
💡 Signal handling complete after receiver executes
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4Final
my_signalundefinedSignal objectSignal with receiver connectedSignal triggeredSignal ready for next use
my_receiverundefinedundefinedConnected to signalCalled by signalFunction execution done
Key Moments - 3 Insights
Why do we connect the receiver function to the signal before sending it?
The receiver must be connected first so Django knows which function to call when the signal is sent, as shown in step 3 and 4 of the execution_table.
What happens if no receiver is connected when the signal is sent?
If no receiver is connected, sending the signal does nothing because no function is called, so no output appears (no step 5 execution).
Why do we pass 'sender' when sending the signal?
The 'sender' identifies who sent the signal; it can be None or any object. Receivers can use it to filter signals, as shown in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed at step 5?
AReceived: Hello
BHello
CSignal sent
DNo output
💡 Hint
Check the 'Result' column in step 5 of the execution_table.
At which step is the receiver function connected to the signal?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the action 'Connect my_receiver to my_signal' in the execution_table.
If we do not call my_signal.send(), what happens?
AReceiver is called anyway
BSignal is connected twice
CReceiver is never called
DError occurs
💡 Hint
Refer to key_moments about what happens if no signal is sent.
Concept Snapshot
Custom signals in Django:
- Define with Signal()
- Connect receiver with signal.connect(func)
- Send signal with signal.send(sender, **kwargs)
- Receiver runs when signal sent
- Use for decoupled event handling
Full Transcript
This visual execution shows how to create and use custom signals in Django. First, a signal object is defined. Then, a receiver function is created and connected to the signal. When the signal is sent with data, Django calls the receiver function, which executes custom code such as printing a message. The variable tracker shows the signal and receiver states at each step. Key moments clarify why connection must happen before sending and what happens if no receiver is connected. The quiz tests understanding of the printed output, connection step, and behavior when the signal is not sent.