🕵️ SicherheitslückenHak5: Hackers Just Poisoned the Rust Supply Chain | Threat Wire(01.09.2026 um 14:00 Uhr)
🕵️ SicherheitslückenHak5: Hackers Found a Way Into Humanoid Robots | Threat Wire(04.09.2026 um 15:04 Uhr)
🔧 AI Nachrichten Bits und so #1021 (Passwort für Laufwerk)(31.08.2026 um 22:15 Uhr)
🔧 AI Nachrichten Bits und so #1022 (Wie Weißbier)(06.09.2026 um 20:39 Uhr)
🍏 iOS / Mac OSHue-App 6.0 ist da: das sind die Neuerungen(07.09.2026 um 17:21 Uhr)
🕵️ SicherheitslückenHak5: Hackers Just Poisoned the Rust Supply Chain | Threat Wire(01.09.2026 um 14:00 Uhr)
🕵️ SicherheitslückenHak5: Hackers Found a Way Into Humanoid Robots | Threat Wire(04.09.2026 um 15:04 Uhr)
🔧 AI Nachrichten Bits und so #1021 (Passwort für Laufwerk)(31.08.2026 um 22:15 Uhr)
🔧 AI Nachrichten Bits und so #1022 (Wie Weißbier)(06.09.2026 um 20:39 Uhr)
🍏 iOS / Mac OSHue-App 6.0 ist da: das sind die Neuerungen(07.09.2026 um 17:21 Uhr)

🔧 Programmierung 🕛 kürzlich 6 Min Lesezeit
0

Building a Flexible Notification System in Django: A Comprehensive Guide

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Notifications are a key component of any modern web application, ensuring users are informed and engaged. A well-implemented notification system can handle multiple channels like in-app alerts, emails, and SMS while dynamically tailoring content for a seamless user experience. In this guide, we’ll walk you through creating a robust, scalable notification system in Django.









System Features



Our notification system is designed to provide:





  1. Support for Multiple Channels: Notifications via in-app alerts, email, or SMS.


  2. Dynamic Content Personalization: Templates with placeholders to generate personalized messages.


  3. Event-Based Triggers: Trigger notifications based on specific system or user events.


  4. Status Tracking: Monitor the delivery status for email and SMS notifications.


  5. Admin and System Integration: Notifications can be triggered by administrators or system events.









Defining the Models






1. Notification Templates



Templates act as the backbone of our system, storing reusable content for notifications.




CODE
from django.db import models

class ChannelType(models.TextChoices):
APP = 'APP', 'In-App Notification'
SMS = 'SMS', 'SMS'
EMAIL = 'EMAIL', 'Email'


class TriggeredByType(models.TextChoices):
SYSTEM = 'SYSTEM', 'System Notification'
ADMIN = 'ADMIN', 'Admin Notification'


class TriggerEvent(models.TextChoices):
ENROLLMENT = 'ENROLLMENT', 'Enrollment'
ANNOUNCEMENT = 'ANNOUNCEMENT', 'Announcement'
PROMOTIONAL = 'PROMOTIONAL', 'Promotional'
RESET_PASSWORD = 'RESET_PASSWORD', 'Reset Password'

class NotificationTemplate(models.Model):
title = models.CharField(max_length=255)
template = models.TextField(help_text='Use placeholders like {{username}} for personalization.')
channel = models.CharField(max_length=20, choices=ChannelType.choices, default=ChannelType.APP)
triggered_by = models.CharField(max_length=20, choices=TriggeredByType.choices, default=TriggeredByType.SYSTEM)
trigger_event = models.CharField(max_length=50, choices=TriggerEvent.choices, help_text='Event that triggers this template.')
is_active = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)






Key Features:





  • template: Text with placeholders for dynamic values like {{username}}.


  • channel: Specifies whether it’s an email, SMS, or in-app notification.


  • trigger_event: Associates the template with a specific event.









2. General Notifications



The Notification model links templates to users and stores any dynamic payload for personalization.




CODE
class Notification(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="notifications")
content = models.ForeignKey(NotificationTemplate, on_delete=models.CASCADE, related_name="notifications")
payload = models.JSONField(default=dict, help_text="Data to replace template placeholders.")
is_read = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)












3. Channel-Specific Models



To handle emails and SMS uniquely, we define specific models.



Email Notifications


This model manages email-specific data, such as dynamic message generation and delivery tracking.




CODE
class StatusType(models.TextChoices):
PENDING = 'PENDING', 'Pending'
SUCCESS = 'SUCCESS', 'Success'
FAILED = 'FAILED', 'Failed'

class EmailNotification(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='email_notifications')
content = models.ForeignKey(NotificationTemplate, on_delete=models.CASCADE, related_name='email_notifications')
payload = models.JSONField(default=dict)
status = models.CharField(max_length=20, choices=StatusType.choices, default=StatusType.PENDING)
status_reason = models.TextField(null=True)

@property
def email_content(self):
"""
Populate the template with dynamic data from the payload.
"""
content = self.content.template
for key, value in self.payload.items():
content = re.sub(
rf"{{{{\s*{key}\s*}}}}",
str(value),
content,
)
return content






SMS Notifications


Similar to email notifications, SMS-specific logic is implemented here.




CODE
class SMSNotification(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='sms_notifications')
content = models.ForeignKey(NotificationTemplate, on_delete=models.CASCADE, related_name='sms_notifications')
payload = models.JSONField(default=dict)
status = models.CharField(max_length=20, choices=StatusType.choices, default=StatusType.PENDING)
status_reason = models.TextField(null=True)

@property
def sms_content(self):
"""
Populate the template with dynamic data from the payload.
"""
content = self.content.template
for key, value in self.payload.items():
content = re.sub(
rf"{{{{\s*{key}\s*}}}}",
str(value),
content,
)
return content












Admin Integration



To make managing notifications easier, we register the models in the Django admin panel.




CODE
from django.contrib import admin
from notifier.models import NotificationTemplate

@admin.register(NotificationTemplate)
class NotificationTemplateAdmin(admin.ModelAdmin):
list_display = ['title', 'channel', 'triggered_by', 'trigger_event', 'is_active']
list_filter = ['channel', 'triggered_by', 'is_active']
search_fields = ['title', 'trigger_event']












Notification Service



We’ll implement a service layer to manage sending notifications through various channels.






Strategy Pattern



Using the Strategy Pattern, we’ll define classes for each notification channel.




CODE
from abc import ABC, abstractmethod
import logging

logger = logging.getLogger(__name__)

class NotificationStrategy(ABC):
@abstractmethod
def send(self, user, content, payload):
pass


class AppNotificationStrategy(NotificationStrategy):
def send(self, user, content, payload):
notification = Notification.objects.create(user=user, content=content, payload=payload)
logger.info(f"In-app notification sent to {user.email}")
return notification


class EmailNotificationStrategy(NotificationStrategy):
def send(self, user, content, payload):
notification = EmailNotification.objects.create(user=user, content=content, payload=payload)
try:
self._send_email(user.email, content.title, notification.email_content)
notification.status = "SUCCESS"
except Exception as e:
notification.status = "FAILED"
notification.status_reason = str(e)
notification.save()
return notification

def _send_email(self, to_email, subject, body):
print(f"Sending email to {to_email} with subject {subject}")
if "@" not in to_email:
raise ValueError("Invalid email address")


class SMSNotificationStrategy(NotificationStrategy):
def send(self, user, content, payload):
notification = SMSNotification.objects.create(user=user, content=content, payload=payload)
try:
self._send_sms(user.phone_number, notification.sms_content)
notification.status = "SUCCESS"
except Exception as e:
notification.status = "FAILED"
notification.status_reason = str(e)
notification.save()
return notification

def _send_sms(self, phone_number, message):
print(f"Sending SMS to {phone_number}: {message}")
if not phone_number.isdigit():
raise ValueError("Invalid phone number")









Notification Service



This service ties everything together, selecting the appropriate strategy based on the notification channel.




CODE
class NotificationService:
_strategies: dict[Type[ChannelType], Type[NotificationStrategy]] = {
ChannelType.APP: AppNotificationStrategy,
ChannelType.EMAIL: EmailNotificationStrategy,
ChannelType.SMS: SMSNotificationStrategy,
}

@classmethod
def get_strategy(cls, instance: NotificationTemplate) -> NotificationStrategy:
try:
channel = ChannelType[instance.channel]
strategy = cls._strategies[channel]
except KeyError:
raise Exception(f"Unknown notification strategy {instance.channel}")

return strategy()

@classmethod
def notify(
cls,
user: User,
event: TriggerEvent,
payload: dict,
):
"""
Automatically create and send a system-triggered notification.

Args:
user: User instance.
event: TriggerEvent type.
payload: Dynamic `dict` data for the notification.

Returns:
Result of the notification strategy.
"""

content, _ = NotificationTemplate.objects.get_or_create(
trigger_event=event,
triggered_by=TriggeredByType.SYSTEM,
defaults={
"title": 'Default Title',
"template": "This is a system notification.",
},
)
strategy = cls.get_strategy(instance=content)
return strategy.send(user, content, payload)












Usage Example



Here’s how you can use the notification service:




CODE
from notifier.services import NotificationService, TriggerEvent

user = User.objects.get(email="[email protected]")
payload = {"username": "John Doe", "course": "Python Basics"}

NotificationService.notify(user, TriggerEvent.USER_ENROLLED, payload)









If you found this guide helpful and insightful, don’t forget to like and follow for more content like this. Your support motivates me to share more practical implementations and in-depth tutorials. Let’s keep building amazing applications together!

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Hackers Just Poisoned the Rust Supply Chain | Threat Wire
1 Quelle
Hackers Found a Way Into Humanoid Robots | Threat Wire
1 Quelle
Bits und so #1021 (Passwort für Laufwerk)
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Building a Flexible Notification System in Django: A Comprehensive Guide

Thematisch verwandte Begriffe: Building, Flexible, Notification, System · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...