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

Django:用户首次登录时展示SQL新对象生成的获奖通知实现方法

在Django中实现用户首次登录时展示触发操作后的通知

1. 设计通知模型

先创建通知模型,核心是标记通知是否已在登录时展示:

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

class UserNotification(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='notifications')
    content = models.TextField()  # 通知内容,例如"恭喜你获得月度最佳创作者奖项!"
    event_type = models.CharField(max_length=50, default='award')  # 标记触发事件类型
    is_displayed = models.BooleanField(default=False)  # 是否已在登录时展示过
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-created_at']

2. 触发操作时创建通知

在用户完成获奖等触发通知的业务逻辑中,生成对应的通知实例:

# 示例:获奖操作的视图函数片段
def grant_award(request):
    # 此处处理获奖的核心逻辑,比如更新用户获奖记录
    target_user = request.user
    # 创建通知
    UserNotification.objects.create(
        user=target_user,
        content=f"恭喜你获得【月度最佳创作者】奖项,奖励已发放至你的账户!"
    )
    return redirect('user_dashboard')

3. 登录后处理通知展示

有两种常用方式实现登录后的通知检查与展示:

方式一:利用登录信号监听

监听Django的用户登录成功信号,在信号处理函数中处理通知:

from django.contrib.auth.signals import user_logged_in
from django.dispatch import receiver
from django.contrib import messages

@receiver(user_logged_in)
def handle_pending_notifications(sender, request, user, **kwargs):
    # 获取当前用户未展示的通知
    pending_notifs = UserNotification.objects.filter(user=user, is_displayed=False)
    if pending_notifs.exists():
        # 将通知内容加入messages框架,方便模板全局渲染
        for notif in pending_notifs:
            messages.success(request, notif.content)
        # 批量标记为已展示,减少数据库操作
        pending_notifs.update(is_displayed=True)

方式二:在登录后首页视图中处理

如果不想用信号,直接在用户登录后进入的首个视图中处理:

def user_dashboard(request):
    current_user = request.user
    pending_notifs = UserNotification.objects.filter(user=current_user, is_displayed=False)
    context = {}
    if pending_notifs.exists():
        # 提取通知内容传递给模板
        context['pending_notifications'] = [notif.content for notif in pending_notifs]
        # 批量更新状态
        pending_notifs.update(is_displayed=True)
    return render(request, 'dashboard.html', context)

4. 模板中渲染通知

根据不同的处理方式,模板渲染逻辑略有区别:

基于messages框架的渲染

在基础模板中添加messages渲染代码,确保所有页面都能显示通知:

{% if messages %}
    <div class="notification-box">
        {% for message in messages %}
            <div class="alert alert-success">{{ message }}</div>
        {% endfor %}
    </div>
{% endif %}

基于视图传递数据的渲染

在dashboard.html中直接渲染弹窗或通知区域:

{% if pending_notifications %}
    <div class="notification-modal">
        <div class="modal-content">
            <h3>新通知</h3>
            {% for content in pending_notifications %}
                <p>{{ content }}</p>
            {% endfor %}
            <button onclick="this.closest('.notification-modal').remove()">关闭</button>
        </div>
    </div>
{% endif %}

注意事项

  • 使用update()批量修改通知状态,避免多次数据库查询,提升性能。
  • 如果需要支持用户后续查看历史通知,可以新增is_read字段,区分首次展示和主动查看状态。
  • 若涉及异步任务触发通知(比如后台定时颁奖),确保任务中正确关联目标用户并创建通知。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 21:54:25