一文教你实战构建消息通知系统Django
本文分享自华为云社区《构建实时消息通知系统: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 构建一个功能强大的消息通知系统,其中涵盖了以下主要内容:
- 通过定义模型、创建信号、编写信号处理器,实现了通知系统的基本框架。
- 集成了前端框架 Bootstrap,并使用 Ajax 技术实现了标记通知为已读的功能,以及实时更新未读通知数量的功能,提升了用户体验。
- 添加了通知删除功能,使用户可以更灵活地管理通知。
- 引入了异步任务处理技术 Celery,将通知的生成和发送操作转换为异步任务,提高了系统的性能和响应速度。
通过这些步骤,我们建立了一个功能完善的消息通知系统,用户可以及时了解到与其相关的重要信息,并且可以自由地管理和处理通知,从而增强了应用的交互性、可用性和性能。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
登录系统演进、便捷登录设计与实现
作者 | 百度APP技术中台吧 导读 随着互联网、物联网和移动终端等技术的迅猛发展,登录认证面临着新的挑战和需求。虽然登录认证在信息系统中是传统且古老的组成部分,但未来的发展前景依然广阔。不论是用户登录、PC端、移动端还是智能设备的访问,身份认证在保障业务操作安全、资金安全、系统间通信和与外部系统集成等多个方面起到至关重要的作用。随着认证方式的不断演进,从最初的cookie和session,发展到如今的多端登录、多因子认证以及API令牌等多种认证手段。同时,用户终端设备的不断升级也推动着认证方式和手段的不断创新。 本文探讨登录认证技术的演进历程,并介绍在终端设备上实现便捷登录的设计与实现方法,以供各位同学参考。我们希望通过这篇分享,能够帮助读者深入了解和应用新型的登录认证技术。 全文4467字,预计阅读时间12分钟。 01登录认证发展历史 账号系统作为一家企业的核心系统,承载着日益增长的业务需求。以百度为例,简化的结构如下: 其账号中心提供支撑服务,支持百度系内100+业务,包括百度APP、网盘、地图、贴吧等,具备全面的账号能力,这对账号系统提出了更高的要求和挑战,那么企业账号管理是如...
- 下一篇
客户案例丨拓数派向量计算引擎PieCloudVector助力东吴证券AIGC应用升级
1.项目背景 随着人工智能技术的不断创新和应用,我们可以看到人工智能在各个领域的应用越来越广泛。深度学习技术在图像识别、语音识别、自然语言处理等领域表现出色。机器学习算法的改进将解决更多实际问题,如增强学习、迁移学习和联合学习等,以更有效地处理复杂的数据问题。自然语言处理技术的不断进步,有助于实现更自然的对话和交流方式,在智能客服、虚拟助手、智能翻译等方面有着广泛应用。数据与AI融合是不可阻挡的历史潮流,大数据和AI技术相互激发、相辅相成,共同推进彼此的发展,两把“金钥匙”握手会再次在金融行业掀起高潮。 AIGC类应用是数智融合的典型,底层基础是强大的数据治理能力,预训练语言大模型不断获得高质量数据进行训练、迭代和优化,从而带来远胜以往的智能应用理念。大模型将激活证券行业的非结构化数据,更高效地释放数据价值,应用将渗透到业务前中后段,带来新的生产力升级。大模型的爆发,将金融业带入到了一个全新时代,但同时也给行业带来了一些难题。 2.现状及痛点 2.1 数据安全问题 涉及敏感信息的业务应用,数据隐私是一个不可忽视的问题。部分场景中存在调用LLMAPI接口服务的情况,不能直接拿取业务数据,...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2整合Redis,开启缓存,提高访问速度
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- MySQL8.0.19开启GTID主从同步CentOS8
- Mario游戏-低调大师作品
- Linux系统CentOS6、CentOS7手动修改IP地址
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS7安装Docker,走上虚拟化容器引擎之路
- Docker快速安装Oracle11G,搭建oracle11g学习环境