使用Django实现信号与消息通知系统【第154篇—Django】

news2024/9/23 19:56:47

👽发现宝藏

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。【点击进入巨牛的人工智能学习网站】。

使用Django实现信号与消息通知系统

在Web应用程序中,实现消息通知系统是至关重要的,它可以帮助用户及时了解到与其相关的事件或动态。Django提供了信号机制,可以用于实现这样的通知系统。本文将介绍如何使用Django的信号机制来构建一个简单但功能强大的消息通知系统,并提供相应的代码和实例。

1. 安装 Django

首先,确保你已经安装了 Django。你可以通过 pip 安装它:

pip install django

2. 创建 Django 项目和应用

创建一个 Django 项目,并在其中创建一个应用:

django-admin startproject notification_system
cd notification_system
python manage.py startapp notifications

3. 定义模型

notifications/models.py 文件中定义一个模型来存储通知信息:

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

class Notification(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    message = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)
    read = models.BooleanField(default=False)

4. 创建信号

notifications/signals.py 文件中创建信号,该信号将在需要发送通知时触发:

from django.dispatch import Signal

notification_sent = Signal(providing_args=["user", "message"])

5. 编写信号处理器

notifications/handlers.py 文件中编写信号处理器,处理信号并创建相应的通知:

from django.dispatch import receiver
from .signals import notification_sent
from .models import Notification

@receiver(notification_sent)
def create_notification(sender, **kwargs):
    user = kwargs['user']
    message = kwargs['message']
    Notification.objects.create(user=user, message=message)

6. 发送通知

在你的应用程序中的适当位置,发送信号以触发通知:

from django.contrib.auth.models import User
from notifications.signals import notification_sent

# 例如,发送通知给特定用户
user = User.objects.get(username='username')
notification_sent.send(sender=None, user=user, message='你有一个新消息')

7. 显示通知

在你的应用程序中,可以通过查询通知模型来显示用户的通知:

from notifications.models import Notification

# 例如,在视图中查询并显示通知
def notifications_view(request):
    user_notifications = Notification.objects.filter(user=request.user)
    return render(request, 'notifications.html', {'notifications': user_notifications})

8. 标记通知为已读

当用户查看通知时,你可能需要将它们标记为已读。你可以在视图中执行此操作:

def mark_as_read(request, notification_id):
    notification = Notification.objects.get(pk=notification_id)
    notification.read = True
    notification.save()
    return redirect('notifications_view')

9. 定义通知模板

创建一个 HTML 模板来呈现通知信息。在 templates/notifications.html 文件中定义:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Notifications</title>
</head>
<body>
    <h1>Notifications</h1>
    <ul>
        {% for notification in notifications %}
        <li{% if notification.read %} style="color: grey;"{% endif %}>
            {{ notification.message }}
            {% if not notification.read %}
            <a href="{% url 'mark_as_read' notification.id %}">Mark as Read</a>
            {% endif %}
        </li>
        {% endfor %}
    </ul>
</body>
</html>

10. 配置 URL

配置 URL 来处理通知相关的请求。在 notification_system/urls.py 文件中:

from django.urls import path
from notifications.views import notifications_view, mark_as_read

urlpatterns = [
    path('notifications/', notifications_view, name='notifications_view'),
    path('notifications/mark_as_read/<int:notification_id>/', mark_as_read, name='mark_as_read'),
]

11. 运行服务器

运行 Django 服务器以查看效果:

python manage.py runserver

现在,你可以访问 http://127.0.0.1:8000/notifications/ 查看通知页面,并且点击“标记为已读”链接来标记通知。

12. 集成前端框架

为了提升通知页面的用户体验,我们可以使用一些流行的前端框架来美化页面并添加一些交互功能。这里以Bootstrap为例。

首先,安装Bootstrap:

pip install django-bootstrap4

settings.py 中配置:

INSTALLED_APPS = [
    ...
    'bootstrap4',
    ...
]

修改通知模板 notifications.html,引入Bootstrap的样式和JavaScript文件,并使用Bootstrap的组件来美化页面:

{% load bootstrap4 %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Notifications</title>
    {% bootstrap_css %}
</head>
<body>
    <div class="container">
        <h1 class="mt-5">Notifications</h1>
        <ul class="list-group mt-3">
            {% for notification in notifications %}
            <li class="list-group-item{% if notification.read %} list-group-item-light{% endif %}">
                {{ notification.message }}
                {% if not notification.read %}
                <a href="{% url 'mark_as_read' notification.id %}" class="btn btn-sm btn-primary ml-2">Mark as Read</a>
                {% endif %}
            </li>
            {% endfor %}
        </ul>
    </div>
    {% bootstrap_javascript %}
</body>
</html>

13. 使用 Ajax 实现标记为已读功能

我们可以使用 Ajax 技术来实现标记通知为已读的功能,这样可以避免刷新整个页面。修改模板文件和视图函数如下:

在模板中,使用 jQuery 来发送 Ajax 请求:

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        $('.mark-as-read').click(function(e) {
            e.preventDefault();
            var url = $(this).attr('href');
            $.ajax({
                type: 'GET',
                url: url,
                success: function(data) {
                    if (data.success) {
                        window.location.reload();
                    }
                }
            });
        });
    });
</script>

修改视图函数 mark_as_read

from django.http import JsonResponse

def mark_as_read(request, notification_id):
    notification = Notification.objects.get(pk=notification_id)
    notification.read = True
    notification.save()
    return JsonResponse({'success': True})

14. 添加通知计数功能

为了让用户可以清晰地知道有多少未读通知,我们可以添加一个通知计数的功能,将未读通知的数量显示在页面上。

首先,在 notifications/views.py 中修改 notifications_view 视图函数:

def notifications_view(request):
    user_notifications = Notification.objects.filter(user=request.user)
    unread_count = user_notifications.filter(read=False).count()
    return render(request, 'notifications.html', {'notifications': user_notifications, 'unread_count': unread_count})

然后,在通知模板中显示未读通知的数量:

<div class="container">
    <h1 class="mt-5">Notifications</h1>
    <div class="alert alert-info mt-3" role="alert">
        You have {{ unread_count }} unread notification(s).
    </div>
    <ul class="list-group mt-3">
        {% for notification in notifications %}
        <li class="list-group-item{% if notification.read %} list-group-item-light{% endif %}">
            {{ notification.message }}
            {% if not notification.read %}
            <a href="{% url 'mark_as_read' notification.id %}" class="btn btn-sm btn-primary ml-2 mark-as-read">Mark as Read</a>
            {% endif %}
        </li>
        {% endfor %}
    </ul>
</div>

15. 实时更新通知计数

为了使通知计数实时更新,我们可以使用 Ajax 技术定期请求服务器以获取最新的未读通知数量。

在通知模板中添加 JavaScript 代码:

<script>
    function updateUnreadCount() {
        $.ajax({
            type: 'GET',
            url: '{% url "unread_count" %}',
            success: function(data) {
                $('#unread-count').text(data.unread_count);
            }
        });
    }
    $(document).ready(function() {
        setInterval(updateUnreadCount, 5000); // 每5秒更新一次
    });
</script>

notifications/urls.py 中添加一个新的 URL 路由来处理未读通知数量的请求:

from django.urls import path
from .views import notifications_view, mark_as_read, unread_count

urlpatterns = [
    path('notifications/', notifications_view, name='notifications_view'),
    path('notifications/mark_as_read/<int:notification_id>/', mark_as_read, name='mark_as_read'),
    path('notifications/unread_count/', unread_count, name='unread_count'),
]

最后,在 notifications/views.py 中定义 unread_count 视图函数:

from django.http import JsonResponse

def unread_count(request):
    user_notifications = Notification.objects.filter(user=request.user, read=False)
    unread_count = user_notifications.count()
    return JsonResponse({'unread_count': unread_count})

16. 添加通知删除功能

除了标记通知为已读之外,有时用户还可能希望能够删除一些通知,特别是一些不再需要的通知。因此,我们可以添加一个删除通知的功能。

首先,在模板中为每个通知添加一个删除按钮:

<ul class="list-group mt-3">
    {% for notification in notifications %}
    <li class="list-group-item{% if notification.read %} list-group-item-light{% endif %}">
        {{ notification.message }}
        <div class="btn-group float-right" role="group" aria-label="Actions">
            {% if not notification.read %}
            <a href="{% url 'mark_as_read' notification.id %}" class="btn btn-sm btn-primary mark-as-read">Mark as Read</a>
            {% endif %}
            <a href="{% url 'delete_notification' notification.id %}" class="btn btn-sm btn-danger delete-notification">Delete</a>
        </div>
    </li>
    {% endfor %}
</ul>

然后,在 notifications/urls.py 中添加一个新的 URL 路由来处理删除通知的请求:

urlpatterns = [
    ...
    path('notifications/delete/<int:notification_id>/', delete_notification, name='delete_notification'),
]

接着,在 notifications/views.py 中定义 delete_notification 视图函数:

def delete_notification(request, notification_id):
    notification = Notification.objects.get(pk=notification_id)
    notification.delete()
    return redirect('notifications_view')

最后,为了使用户可以通过 Ajax 删除通知,我们可以修改模板中的 JavaScript 代码:

<script>
    $(document).ready(function() {
        $('.delete-notification').click(function(e) {
            e.preventDefault();
            var url = $(this).attr('href');
            $.ajax({
                type: 'GET',
                url: url,
                success: function(data) {
                    if (data.success) {
                        window.location.reload();
                    }
                }
            });
        });
    });
</script>

17. 添加异步任务处理

在实际应用中,通知系统可能需要处理大量的通知,而生成和发送通知可能是一个耗时的操作。为了避免阻塞用户请求,我们可以使用异步任务处理来处理通知的生成和发送。

17.1 安装 Celery

首先,安装 Celery 和 Redis 作为消息代理:

pip install celery redis
17.2 配置 Celery

在 Django 项目的根目录下创建一个名为 celery.py 的文件,并添加以下内容:

import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'notification_system.settings')

app = Celery('notification_system')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

settings.py 中添加 Celery 配置:

CELERY_BROKER_URL = 'redis://localhost:6379/0'
17.3 创建异步任务

notifications/tasks.py 中定义异步任务来处理通知的生成和发送:

from celery import shared_task
from .models import Notification

@shared_task
def send_notification(user_id, message):
    user = User.objects.get(pk=user_id)
    Notification.objects.create(user=user, message=message)
17.4 触发异步任务

在你的应用程序中,当需要发送通知时,使用 Celery 的 delay() 方法触发异步任务:

from notifications.tasks import send_notification

send_notification.delay(user_id, '你有一个新消息')

总结:

本文介绍了如何使用 Django 构建一个功能强大的消息通知系统,其中涵盖了以下主要内容:

  1. 通过定义模型、创建信号、编写信号处理器,实现了通知系统的基本框架。
  2. 集成了前端框架 Bootstrap,并使用 Ajax 技术实现了标记通知为已读的功能,以及实时更新未读通知数量的功能,提升了用户体验。
  3. 添加了通知删除功能,使用户可以更灵活地管理通知。
  4. 引入了异步任务处理技术 Celery,将通知的生成和发送操作转换为异步任务,提高了系统的性能和响应速度。

通过这些步骤,我们建立了一个功能完善的消息通知系统,用户可以及时了解到与其相关的重要信息,并且可以自由地管理和处理通知,从而增强了应用的交互性、可用性和性能。

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1542575.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Hadoop大数据技术】——Hadoop高可用集群(学习笔记)

&#x1f4d6; 前言&#xff1a;Hadoop设计之初&#xff0c;在架构设计和应用性能方面存在很多不如人意的地方&#xff0c;如HDFS和YARN集群的主节点只能有一个&#xff0c;如果主节点宕机无法使用&#xff0c;那么将导致HDFS或YARN集群无法使用&#xff0c;针对上述问题&#…

网络协议栈--传输层--UDP/TCP协议

目录 本节重点一、再谈端口号1.1 再谈端口号1.2 端口号范围划分1.3 认识知名端口号(Well-Know Port Number)1.4 回答两个问题1.5 netstat1.6 pidof 二、UDP协议2.1 UDP协议段格式2.2 UDP的特点2.3 面向数据报2.4 UDP的缓冲区2.5 UDP使用注意事项2.6 基于UDP的应用层协议2.7 UDP…

【Linux】nmcli命令详解

目录 ​编辑 一、概述 二、常用参数使用 2.1 nmcli networking 1.显示NM是否接管网络 2.查看网络连接状态 3.开/关网络连接 2.2 general ​编辑 1.显示系统网络状态 2.显示主机名 3.更改主机名 2.3 nmcli connection ​编辑1.显示所有网络连接 2.显示某个网卡的…

基于ssm的酒店民宿管理系统的设计与实现

系统主要功能介绍&#xff1a; 1、登录&#xff1a;输入账号密码进行登录&#xff0c;登录后才能进行相应的操作 2、客房管理&#xff1a;客房管理主要是酒店预订&#xff0c;可以选择不同的房间&#xff0c;比如大床房&#xff0c;家庭房等&#xff0c;入住办理&#xff0c;…

DC电源模块的设计与调试技巧

BOSHIDA DC电源模块的设计与调试技巧 DC电源模块的设计与调试是电子工程师在实际项目中常常需要面对的任务。一个稳定可靠的DC电源模块对于电路的正常运行起到至关重要的作用。以下是一些设计与调试的技巧&#xff0c;帮助工程师们更好地完成任务。 第一&#xff0c;正确选择…

【项目管理后台】Vue3+Ts+Sass实战框架搭建二

Vue3TsSass搭建 git cz的配置mock 数据配置viteMockServe 建立mock/user.ts文件夹测试一下mock是否配置成功 axios二次封装解决env报错问题&#xff0c;ImportMeta”上不存在属性“env” 统一管理相关接口新建api/index.js 路由的配置建立router/index.ts将路由进行集中封装&am…

2014年发射的SAR卫星传感器

2014年有多颗SAR卫星发射。包括Sentinel-1A、ALOS-2、SAOCOM 1A、SMAP。 Sentinel-1A 欧空局&#xff08;ESA&#xff09;的Sentinel-1A卫星发射于2014年4月3号&#xff0c;计划发射两颗&#xff0c;载荷为C波段SAR。 Sentinel-1延续了ERS-2和Envisat的观测任务。它具有更快…

宝贝的甜蜜梦乡:新生儿睡眠的温馨指南

引言&#xff1a; 新生儿的睡眠是他们健康成长的重要组成部分&#xff0c;良好的睡眠不仅有助于宝宝的身体发育&#xff0c;还对他们的认知和情绪发展至关重要。然而&#xff0c;新生儿的睡眠模式与成人不同&#xff0c;需要家长们特别关注和照顾。本文将为您介绍新生儿睡眠时间…

linux之zabbix自定义监控

zabbix基本配置见&#xff1a;写文章-CSDN创作中心https://mp.csdn.net/mp_blog/creation/editor/136783672 自定义监控规则 命令为who | wc -l 显示为2&#xff0c;主机一个&#xff0c;mobaxterm一个&#xff0c;思路是开启3个终端&#xff0c;让主机的zabbix服务自动检测1…

day6:STM32MP157——串口通信实验

使用的是cortex A7内核 【串口通信的工作原理】 本次实验使用的是uart4的串口&#xff0c;分别使用了uart4_tx和uart4_rx两个引脚。根据板子的原理图我们可以知道&#xff0c;他们分别对应着芯片的PG11和PB2 从引脚名字也可以知道使用了GPIO口&#xff0c;所以本次实验同样需…

neo4j所有关系只显示RELATION,而不显示具体的关系

当看r时&#xff0c;真正的关系在properties中的type里&#xff0c;而type为“RELATION” 造成这个的原因是&#xff1a; 在创建关系时&#xff0c;需要指定关系的类型&#xff0c;这是固定的&#xff0c;不能像属性那样从CSV文件的一个字段动态赋值。标准的Cypher查询语言不支…

【MyBatis】初始化过程

MyBatis 初始化过程 可以分为以下几个步骤&#xff1a; 添加依赖&#xff1a;首先&#xff0c;需要在项目的 pom.xml&#xff08;如果是 Maven 项目&#xff09;或 build.gradle&#xff08;如果是 Gradle 项目&#xff09;文件中添加 MyBatis 以及数据库驱动的依赖。 配置文…

Java集合框架初学者指南:List、Set与Map的实战训练

Java集合框架是Java语言的核心部分&#xff0c;它提供了丰富的类和接口&#xff0c;用来高效地管理和操作大量数据。这个强大的工具箱包括多种集合类型&#xff0c;其中最为常用的是List、Set和Map。 1.List - 有序且可重复的数据清单 概念&#xff1a; List就像一个购物清单&…

Qt程序可执行文件打包

目录 一、新建一个目录二、命令行2.1 添加临时变量2.2 打包命令 三、添加动态库四、普通 Qt 项目打包 Qml 项目打包 笔者写的python程序打包地址&#xff08;https://blog.csdn.net/qq_43700779/article/details/136994813&#xff09; 一、新建一个目录 新目录(例如test)用以…

Spring Boot整合Spring Security

Spring Boot 专栏&#xff1a;Spring Boot 从零单排 Spring Cloud 专栏&#xff1a;Spring Cloud 从零单排 GitHub&#xff1a;SpringBootDemo Gitee&#xff1a;SpringBootDemo Spring Security是针对Spring项目的安全框架&#xff0c;也是Spring Boot底层安全模块的默认技术…

基于springboot+vue+Mysql的留守儿童爱心网站

开发语言&#xff1a;Java框架&#xff1a;springbootJDK版本&#xff1a;JDK1.8服务器&#xff1a;tomcat7数据库&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09;数据库工具&#xff1a;Navicat11开发软件&#xff1a;eclipse/myeclipse/ideaMaven包&#xff1a;…

学习周报:文献阅读+Fluent案例+水力学理论学习

目录 摘要 Abstract 文献阅读&#xff1a; 文献摘要 现有问题 研究目的及方法 PINN的设置 NS方程介绍 损失函数 训练方法 实验设置 对照组设置 实验结果展示 点云数、隐藏层数和每个隐藏层的节点数对PINN精度的影响 点云数对PINN的影响&#xff1a; 隐藏层数的影…

Kruskal最小生成树【详细解释+动图图解】【sort中的cmp函数】 【例题:洛谷P3366 【模板】最小生成树】

文章目录 Kruskal算法简介Kruskal算法前置知识sort 中的cmp函数 算法思考样例详细示范与解释kruskal模版code↓ 例题&#xff1a;洛谷P3366 【模板】最小生成树code↓完结撒花QWQ Kruskal算法简介 K r u s k a l Kruskal Kruskal 是基于贪心算法的 M S T MST MST 算法&#xff…

探索国内ip切换App:打破网络限制

在国内网络环境中&#xff0c;有时我们会遇到一些限制或者屏蔽&#xff0c;使得我们无法自由访问一些网站或服务。而国内IP切换App的出现&#xff0c;为解决这些问题提供了非常便捷的方式。这些App可以帮助用户切换IP地址&#xff0c;让用户可以轻松地访问被限制或屏蔽的网站&a…

【计算机考研】 跨考408全年复习规划+资料分享

跨专业备考计算机考研408&#xff0c;确实是一项挑战。在有限的时间内&#xff0c;我们需要合理安排时间&#xff0c;制定有效的学习计划&#xff0c;做到有效地备考。回顾我之前对408的经验&#xff0c;我想分享一些备考计划和方法。 要认清自己的起点。作为跨专业考生&#…