0
0
Djangoframework~20 mins

Custom signals in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Signals Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a custom signal is sent without any receivers?
Consider a Django custom signal defined and sent, but no receiver functions are connected to it. What is the behavior when the signal is sent?
Django
from django.dispatch import Signal

my_signal = Signal()

my_signal.send(sender=None)
AThe signal send method returns an empty list, and no error occurs.
BA RuntimeError is raised because no receivers are connected.
CThe signal send method returns None and logs a warning.
DThe signal send method raises a TypeError due to missing sender argument.
Attempts:
2 left
💡 Hint
Think about what happens if no one is listening when you shout.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a custom Django signal with providing arguments?
You want to create a custom Django signal that sends two arguments: 'user' and 'action'. Which code snippet correctly defines this signal?
Amy_signal = Signal(provides=['user', 'action'])
Bmy_signal = Signal(providing=['user', 'action'])
Cmy_signal = Signal(args=['user', 'action'])
Dmy_signal = Signal(providing_args=['user', 'action'])
Attempts:
2 left
💡 Hint
Check the exact keyword argument name for declaring arguments in Django signals.
🔧 Debug
advanced
3:00remaining
Why does this custom signal receiver not get called?
Given the code below, why does the receiver function never execute when the signal is sent?
Django
from django.dispatch import Signal, receiver

my_signal = Signal(providing_args=['data'])

@receiver(my_signal)
def my_receiver(sender, **kwargs):
    print('Received:', kwargs.get('data'))

# Somewhere else in code
my_signal.send(sender=None, data='Hello')
AThe signal must be sent with a sender class, not None.
BThe receiver is not connected because the module defining it was never imported.
CThe signal send call is missing the required sender argument.
DThe receiver function signature is incorrect; it must accept 'data' explicitly.
Attempts:
2 left
💡 Hint
Think about how Django connects signals and when decorators run.
state_output
advanced
3:00remaining
What is the output of this custom signal with multiple receivers?
Given the code below, what will be printed when the signal is sent?
Django
from django.dispatch import Signal, receiver

my_signal = Signal(providing_args=['value'])

@receiver(my_signal)
def receiver_one(sender, **kwargs):
    print('One:', kwargs.get('value'))
    return 'one'

@receiver(my_signal)
def receiver_two(sender, **kwargs):
    print('Two:', kwargs.get('value'))
    return 'two'

results = my_signal.send(sender=None, value=42)
print(results)
A[(<function receiver_one at ...>, 'one'), (<function receiver_two at ...>, 'two')]
B[('receiver_one', 'one'), ('receiver_two', 'two')]
C[(receiver_one, 'one'), (receiver_two, 'two')]
D[(<function receiver_one>, 'one'), (<function receiver_two>, 'two')]
Attempts:
2 left
💡 Hint
Recall what the send method returns: a list of tuples with receiver references and their return values.
🧠 Conceptual
expert
2:30remaining
Which statement about Django custom signals is TRUE?
Select the only true statement about Django custom signals.
ACustom signals must always specify a sender class; None is not allowed.
BSignal receivers can modify the sender argument before other receivers are called.
CThe send method returns a list of tuples with each receiver and its return value.
DDjango automatically connects custom signals to model save events.
Attempts:
2 left
💡 Hint
Think about what the send method returns after notifying receivers.