← Back to Chapters

Django Signals

⚡ Django Signals

? Quick Overview

Django signals allow you to automatically perform actions in response to specific events such as saving or deleting a model. They help keep your code modular, clean, and event-driven.

? Key Concepts

  • Signals listen for events like post_save and post_delete
  • Receivers execute logic automatically when the event occurs
  • Ideal for automation and decoupled logic

? Syntax / Theory

A signal connects a sender (model) with a receiver (function). When the sender triggers an event, Django executes the receiver automatically.

? Code Examples

? Example 1: Creating User Profiles Automatically

? View Code Example
# Automatically create a profile when a new user is created
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User
from .models import Profile

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

? Example 2: Sending Welcome Email After Registration

? View Code Example
# Send a welcome email after user registration
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.core.mail import send_mail
from django.contrib.auth.models import User

@receiver(post_save, sender=User)
def send_welcome_email(sender, instance, created, **kwargs):
    if created:
        send_mail(
            'Welcome to Our Website',
            'Thank you for registering with us!',
            'from@example.com',
            [instance.email],
            fail_silently=False,
        )

? Example 3: Cleaning Up Files on Deletion

? View Code Example
# Delete user profile picture after user deletion
from django.db.models.signals import post_delete
from django.dispatch import receiver
from django.contrib.auth.models import User
import os

@receiver(post_delete, sender=User)
def delete_user_files(sender, instance, **kwargs):
    if instance.profile.picture:
        path = instance.profile.picture.path
        if os.path.exists(path):
            os.remove(path)

? Live Output / Explanation

  • User profiles are created automatically on registration
  • Emails are sent without manual triggers
  • Files are safely removed when records are deleted

? Interactive Concept

Think of signals as automatic hooks that react to model events without being explicitly called in views or forms.

? Signal Flow Simulator

Click the button to simulate a user signing up. Watch how the database save triggers the signal, which then triggers the receivers automatically.

Waiting for action... Click simulate!

? Use Cases

  • Creating profiles, logs, or notifications
  • Sending emails on registration or updates
  • Cleaning files and resources on deletion
  • Syncing related models automatically

✅ Tips & Best Practices

  • Keep signal logic lightweight and focused
  • Use signals only for cross-cutting concerns
  • Prefer services or tasks for heavy processing

? Try It Yourself

  • Create a signal to log user activity
  • Send a custom email on profile update
  • Delete uploaded files using post_delete