receiver(signal)
[source]
Parameters: | signal – A signal or a list of signals to connect a function to. |
---|
Here’s how you connect with the decorator:
from django.core.signals import request_finished from django.dispatch import receiver @receiver(request_finished) def my_callback(sender, **kwargs): print("Request finished!")
Now, our my_callback
function will be called each time a request finishes.
Where should this code live?
Strictly speaking, signal handling and registration code can live anywhere you like, although it’s recommended to avoid the application’s root module and its models
module to minimize side-effects of importing code.
In practice, signal handlers are usually defined in a signals
submodule of the application they relate to. Signal receivers are connected in the ready()
method of your application configuration class. If you’re using the receiver()
decorator, simply import the signals
submodule inside ready()
.
Note
The ready()
method may be executed more than once during testing, so you may want to guard your signals from duplication, especially if you’re planning to send them within tests.
Please login to continue.