Django framework has a concept of signals, which are similar to events in JavaScript. There is a handful of built-in signals that you can use to trigger actions before and after initialization of a model, saving or deleting an instance, migrating the database schema, handling a request, and so on. Moreover, you can create your own signals in your reusable apps and handle them in other apps. In this recipe, you will learn how to use signals to send emails to administrators whenever a specific model is saved.
Let's start with the viral_videos
app that we created in the Using database query expressions recipe.
Follow these steps to create notifications to administrators:
Create the
signals.py
file with the following content:# viral_videos/signals.py # -*- coding: UTF-8 -*- from __future__ import unicode_literals from django.db.models.signals import post_save from django.dispatch import receiver from .models import...