Django Signals
Django Signals#
Well what are they? They are hooks that you can listen for in your application and do a specific task when they occur.
You can check the Signal Docs for a more detailed explanation
There are alot of built-in signals
Using Signals Example with User Authentication Signals#
In this example we will be catching the user logout signal with the django.contrib.auth.signal.user_logged_out
and adding a django message
NB First thing to do is make sure the app knows about the signals
-
Create a
signals.py
file -
You need to import your signals into
<app_label>/apps.py
from django.apps import AppConfig class UsersConfig(AppConfig): name = 'users' def ready(self): import users.signals
-
Make sure the Config is loaded in
__init__.py
:default_app_config = '<app_name>.apps.<AppConfig>'
-
In
signals.py
create your signal receiverRemember the parameters available are specified in built-in signal
from django.contrib.auth.signals import user_logged_out from django.dispatch import receiver @receiver(user_logged_out) def add_message_on_logout(sender, request, user, **kwargs): messages.info(request, 'You have been logged out.')
Alternatively you can connect to signals without the decorator
:
def add_message_on_logout(sender, request, user, **kwargs):
messages.info(request, 'You have been logged out.')
user_logged_out.connect(add_message_on_logout)