You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于Django构建通知系统?无需Django Channels可行吗?

Hey there! Let's tackle your questions about building a notification system in Django—no jargon, just practical steps and solutions.

1. 如何基于Django框架构建通知系统?

Building a basic notification system in Django is straightforward, and we can break it down into 4 core steps:

Step 1: Design the Notification Model

First, you need a database model to store all notification data. This model should track key details like the recipient, content, read status, and timestamp. Here's a robust example:

from django.db import models
from django.contrib.auth.models import User

class Notification(models.Model):
    recipient = models.ForeignKey(User, on_delete=models.CASCADE, related_name='notifications')
    content = models.TextField()
    is_read = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    # Optional: Add a type field to categorize notifications (likes, comments, follows)
    NOTIFICATION_TYPES = (
        ('LIKE', 'Like'),
        ('COMMENT', 'Comment'),
        ('FOLLOW', 'Follow'),
    )
    type = models.CharField(max_length=10, choices=NOTIFICATION_TYPES)

    class Meta:
        ordering = ['-created_at']  # Show newest notifications first

Step 2: Trigger Notifications

Next, you need to create notifications when specific events happen (e.g., someone comments on a user's post). Here's how you'd add this logic to a comment submission view:

from django.shortcuts import get_object_or_404, redirect
from .models import Post, Comment, Notification
from .forms import CommentForm

def add_comment(request, post_id):
    post = get_object_or_404(Post, id=post_id)
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.author = request.user
            comment.post = post
            comment.save()
            
            # Send notification to the post author (avoid self-notifications)
            if post.author != request.user:
                Notification.objects.create(
                    recipient=post.author,
                    content=f"{request.user.username} commented on your post: {comment.content[:50]}...",
                    type='COMMENT'
                )
            return redirect('post_detail', post_id=post.id)
    # Handle GET requests or invalid forms
    form = CommentForm()
    return render(request, 'posts/add_comment.html', {'form': form, 'post': post})

Step 3: Display Notifications to Users

Create a view to fetch the current user's notifications, then render them in a template. Here's the view:

from django.contrib.auth.decorators import login_required
from django.shortcuts import render

@login_required
def user_notifications(request):
    notifications = request.user.notifications.all()
    unread_count = notifications.filter(is_read=False).count()
    return render(request, 'notifications/list.html', {
        'notifications': notifications,
        'unread_count': unread_count
    })

And a simple template (notifications/list.html) to show the notifications:

<h2>Your Notifications</h2>
{% if unread_count > 0 %}
    <p>You have <strong>{{ unread_count }}</strong> unread notifications</p>
{% endif %}
<ul class="notification-list">
    {% for notification in notifications %}
        <li class="{% if not notification.is_read %}unread{% endif %}">
            {{ notification.content }}
            <small class="timestamp">{{ notification.created_at|timesince }} ago</small>
            {% if not notification.is_read %}
                <a href="{% url 'mark_notification_read' notification.id %}">Mark as read</a>
            {% endif %}
        </li>
    {% empty %}
        <li>No notifications yet—start interacting with other users!</li>
    {% endfor %}
</ul>

Step 4: Mark Notifications as Read

Add a view to let users mark individual notifications as read:

from django.shortcuts import get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from .models import Notification

@login_required
def mark_notification_read(request, notification_id):
    notification = get_object_or_404(Notification, id=notification_id, recipient=request.user)
    notification.is_read = True
    notification.save()
    return redirect('user_notifications')

Don't forget to add the URL patterns for these views in your urls.py!

2. 是否可在不使用Django Channels的前提下,为基于Django搭建的网站构建通知系统?若可行,具体实现方式是什么?

Absolutely! Django Channels is great for real-time WebSocket-based notifications, but it's not a requirement. If you don't need instant push notifications (or can accept "near-real-time" updates), you have several solid options:

Option 1: Frontend Polling (Easiest Implementation)

This is the simplest approach—your frontend sends a request to the backend at regular intervals (e.g., every 10 seconds) to check for new notifications.

Frontend (using vanilla JavaScript):

// Check for unread notifications every 10 seconds
setInterval(() => {
    fetch('/api/notifications/unread-count/')
        .then(response => response.json())
        .then(data => {
            const badge = document.getElementById('notification-badge');
            if (data.count > 0) {
                badge.textContent = data.count;
                badge.style.display = 'inline';
            } else {
                badge.style.display = 'none';
            }
        });
}, 10000); // 10-second interval

Backend View:

from django.http import JsonResponse
from django.contrib.auth.decorators import login_required

@login_required
def unread_notification_count(request):
    count = request.user.notifications.filter(is_read=False).count()
    return JsonResponse({'count': count})

Pros: No extra dependencies, super easy to set up.
Cons: Small delay between notification creation and user seeing it; slightly increased server load (adjust the interval to balance this).

Option 2: Asynchronous Task + Email/SMS Notifications

If you want to notify users outside the website (e.g., via email), use an asynchronous task runner like Celery to send notifications in the background—no Channels needed.

First, set up Celery with Django, then create a task:

from celery import shared_task
from django.core.mail import send_mail
from .models import Notification

@shared_task
def send_notification_email(notification_id):
    notification = Notification.objects.get(id=notification_id)
    subject = f"New {notification.type} Notification"
    message = f"Hi {notification.recipient.username},\n\n{notification.content}\n\nView all notifications here: https://your-site.com/notifications/"
    send_mail(subject, message, 'noreply@your-site.com', [notification.recipient.email])

Then trigger the task when creating a notification:

# In the add_comment view from earlier
notification = Notification.objects.create(
    recipient=post.author,
    content=f"{request.user.username} commented on your post: {comment.content[:50]}...",
    type='COMMENT'
)
send_notification_email.delay(notification.id)

Option 3: Use a Third-Party Library

Libraries like django-notifications-hq handle all the heavy lifting for you—no need to build the model, views, or templates from scratch. It doesn't require Channels and supports all the core features you'd need.

  1. Install the library:
pip install django-notifications-hq
  1. Add it to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
    # ...
    'notifications',
]
  1. Run migrations:
python manage.py migrate
  1. Trigger a notification:
from notifications.signals import notify

notify.send(
    request.user,  # Sender
    recipient=post.author,  # Recipient
    verb='commented on',
    action_object=comment,
    description=f"your post: {post.title}"
)
  1. Display notifications in templates using built-in tags:
{% load notifications_tags %}
{% notifications_unread as unread_count %}

{% if unread_count %}
    <a href="{% url 'notifications:unread' %}">You have {{ unread_count }} unread notifications</a>
{% endif %}

This is perfect if you want to get a production-ready notification system up quickly.


内容的提问来源于stack exchange,提问作者Adi Nishad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 14:57:56