0
0
Djangoframework~10 mins

Receiver decorator in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Receiver decorator
Define signal handler function
Apply @receiver decorator with signal
Connect handler to signal
Signal sent somewhere in app
Django calls connected handler
Handler executes with signal data
Shows how a function is decorated to listen for a signal, then called automatically when that signal is sent.
Execution Sample
Django
from django.dispatch import receiver, Signal

my_signal = Signal()

@receiver(my_signal)
def my_handler(sender, **kwargs):
    print('Signal received')
Defines a signal and a handler function that prints a message when the signal is received.
Execution Table
StepActionEvaluationResult
1Define my_signal as a new Signalmy_signal createdSignal object ready
2Define my_handler functionFunction createdFunction exists but not connected
3Apply @receiver(my_signal) decoratorConnect my_handler to my_signalmy_handler registered as listener
4Send my_signalDjango finds connected handlersmy_handler called
5my_handler runsPrint 'Signal received'Message output to console
6No more handlersSignal processing endsExecution complete
💡 All connected handlers called; signal processing finished
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4Final
my_signalundefinedSignal objectSignal objectSignal objectSignal object
my_handlerundefinedFunction objectConnected functionConnected functionConnected function
Key Moments - 2 Insights
Why does the handler function run only after the signal is sent?
Because the @receiver decorator only connects the function to the signal; the function runs when Django sends the signal (see execution_table step 4 and 5).
Can the handler function be called without sending the signal?
No, the handler runs only when the signal is sent. Defining and connecting it does not execute it immediately (see execution_table steps 2 and 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 3?
AThe handler function prints output
BThe handler function is connected to the signal
CThe signal is sent
DThe signal object is created
💡 Hint
Check the 'Action' and 'Result' columns at step 3 in the execution_table
At which step does the handler function actually run?
AStep 5
BStep 4
CStep 2
DStep 1
💡 Hint
Look for when the handler prints output in the execution_table
If the signal is never sent, what happens to the handler function?
AIt runs immediately after definition
BIt runs when the app starts
CIt never runs
DIt runs twice
💡 Hint
Refer to the variable_tracker and execution_table steps 4 and 5
Concept Snapshot
Receiver decorator in Django:
- Use @receiver(signal) above a function
- Connects function to listen for that signal
- Function runs only when signal is sent
- Handler receives sender and kwargs
- Useful for decoupled event handling
Full Transcript
The receiver decorator in Django is used to connect a function to a signal. First, you define a signal object. Then, you write a handler function that will respond to that signal. By applying the @receiver decorator with the signal as argument, Django registers the function as a listener. When the signal is sent somewhere in the app, Django automatically calls the connected handler function, passing the sender and any extra data. The handler then executes its code, such as printing a message. This allows different parts of the app to communicate without tight coupling. The handler only runs when the signal is sent, not before.