Django includes a package to manage the application's Signals, with which it is possible that the application can receive notifications when certain actions occur in the application, such as creating a post, registering a user, etc, when any of these actions happen, we can trigger this signal to execute a function that performs some action such as recording a log, sending an email, among others.
Signals are nothing more than configurable events or messengers that report important events in our application, they are a form of communication between different parts of the application. In this chapter, we'll explain what Signals are, how they work, and how you can take advantage of them to keep your code organized and efficient.
Signals are a form of communication between different parts of your application. With signals, it is possible to notify a set of receivers that something has happened.
As an example, assuming we have a blog and you want to send an email every time someone registers, we can use Signals in Django, the event is the registration of the event that triggers a signal that is connected to a function that is the one that send the email; this action can be any other operation such as sending a notification or another custom task.
The use of signs is very easy, we just have to create a function that must be executed when the event happens, this function is the receive or the one that receives, and we specify it using the from django.dispatch import receiver decorator which, we establish two parameters, the signal type and the model.
We have different types of signals, you can use all of them to perform personalized actions before/after performing the corresponding actions and we can classify them into five types:
Signals related to models:
Signals related to migrations:
Signals related to HTTP requests and responses:
Signals related to unit tests:
Signals related to database connections:
When receiving these signals it has specific parameters that you can explore in the official documentation:
https://docs.djangoproject.com/en/dev/ref/signals/
The first thing we must do is define a Signal, for this we use a function that is activated when a specific event occurs, for example, the creation of an alert:
channels/alert/signals.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import Alert
@receiver(post_save, sender=Alert)
def save_user_profile(sender, instance, **kwargs):
print('Alert Created')
This function will act as a receiver of the signal, that is, it will be executed when the corresponding signal is executed, in this example, the corresponding signal is the creation (post_save) of an alert, it will display a message through the console, but, the task to perform can be any other operation such as sending an email or other database operations.
In the apps.py file of your application, we must register the signals.py file in the ready() method:
channels/alert/apps.py
from django.apps import AppConfig
class AlertConfig(AppConfig):
name = 'alert'
def ready(self):
import alert.signals
In this example method, we create an alert:
channels/alert/views.py
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from .models import Alert
@login_required
def create(request):
alert = Alert()
alert.content = 'Test'
alert.user = request.user
alert.save()
return HttpResponse('Response')
And we create the path of the previous view:
channels/alert/urls.py
urlpatterns = path('dashboard/create',views.create),
Now, every time we create an alert, the previous signal will be automatically executed, and this is an important factor, every time an alert is created, for example, from Django Channels, or through the consumer that we created in the previous section, the previous signal will be executed, as we mentioned previously, the signal is nothing more than a function associated with an event except that the signal is executed automatically when the event occurs.
Official documentation:
https://docs.djangoproject.com/en/dev/topics/signals/
https://docs.djangoproject.com/en/dev/ref/signals/
- Andrés Cruz
This material is part of my complete course and book; You can purchase them from the books and/or courses section, Curso y libro desarrollo web con Django 5 y Python 3 + integración con Vue 3, Bootstrap y Alpine.js.
Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter
I agree to receive announcements of interest about this Blog.
!Courses from!
4$
In Academy
View courses!Books from!
1$
See the books