0
0
Djangoframework~10 mins

Custom signals in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Django signal dispatcher.

Django
from django.dispatch import [1]
Drag options to blanks, or click blank then click option'
ASignal
Breceiver
Csend
Dconnect
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'receiver' instead of 'Signal'.
Trying to import 'send' which is a method, not a class.
2fill in blank
medium

Complete the code to define a new custom signal named order_completed.

Django
order_completed = [1]()
Drag options to blanks, or click blank then click option'
ASignal
Breceiver
Csend
Dconnect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'receiver' which is a decorator, not a signal.
Trying to call 'send' which is a method to send signals.
3fill in blank
hard

Fix the error in the receiver function decorator to connect it to the order_completed signal.

Django
@[1](order_completed)
def notify_user(sender, **kwargs):
    print('Order completed!')
Drag options to blanks, or click blank then click option'
ASignal
Breceiver
Csend
Dconnect
Attempts:
3 left
💡 Hint
Common Mistakes
Using '@Signal' which is a class, not a decorator.
Using '@send' which is a method, not a decorator.
4fill in blank
hard

Fill both blanks to send the order_completed signal with the sender as the Order class.

Django
order_completed.[1](sender=[2])
Drag options to blanks, or click blank then click option'
Asend
BOrder
Cconnect
Dreceiver
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'connect' instead of 'send' to trigger the signal.
Passing an instance instead of the class as sender.
5fill in blank
hard

Fill all three blanks to define a receiver function that listens to order_completed and prints the order ID from kwargs.

Django
@[1](order_completed)
def handle_order(sender, **[2]):
    print(f"Order ID: [3]['order_id']")
Drag options to blanks, or click blank then click option'
Areceiver
Bkwargs
Dargs
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'args' instead of 'kwargs' to get keyword arguments.
Forgetting the '@receiver' decorator.