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

Django通知模板仅显示第3项后内容,前项为空<p>标签求助

问题描述

通知模板仅渲染第3个位置之后的通知项,检查前端元素发现前几项存在但仅为空白的HTML <p>标签;其余功能正常,寻求解决方法。

问题截图

问题截图

相关代码

models.py

class Notification(models.Model):
    MESSAGE = 'message'
    APPLICATION = 'application'

    CHOICES = (
        (MESSAGE, 'Message'),
        (APPLICATION, 'Application')
    )

    to_user = models.ForeignKey(User, related_name='notifications', on_delete=models.CASCADE)
    notification_type = models.CharField(max_length=20, choices=CHOICES)
    is_read = models.BooleanField(default=False)
    extra_id = models.IntegerField(null=True, blank=True)

    created_at = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User, related_name='creatednotifications', on_delete=models.CASCADE)

    class Meta:
        ordering = ['-created_at']

views.py(通知模块)

@login_required
def notifications(request):
    goto = request.GET.get('goto', '')
    notification_id = request.GET.get('notification', 0)
    extra_id = request.GET.get('extra_id', 0)

    if goto != '':
        notification = Notification.objects.get(pk=notification_id)
        notification.is_read = True
        notification.save()

        if notification.notification_type == Notification.MESSAGE:
            return redirect('room', pk=notification.extra_id)
        elif notification.notification_type == Notification.APPLICATION:
            return redirect('room', pk=notification.extra_id)
    
    return render(request, 'notification/notifications.html')

notifications.html模板

{% extends 'main.html' %}

{% block content %}
    <div class="container">
        <h1 class="title">Notifications</h1>
        

        {% if not notifications %}
            No notifications yet!
        {% endif %}

        {% for notification in notifications %}
            <div class="notification">
                <p>
                    {% if notification.notification_type == 'message' %}
                        <a href="{% url 'notifications' %}?goto=room&notification={{ notification.id }}&extra_id={{ notification.extra_id }}">
                            <strong>{{ notification.created_by.username }}</strong> sent you a message<br>
                            <small>{{ notification.created_at|timesince }} ago</small>
                        </a>
                    {% elif notification.notification_type == 'application' %}
                        <a href="{% url 'notifications' %}?goto=room&notification={{ notification.id }}&extra_id={{ notification.extra_id }}">
                            <strong>{{ notification.created_by.username }}</strong> created a room<br>
                            <small>{{ notification.created_at|timesince }} ago</small>
                        </a>
                    {% endif %}
                </p>
            </div>
        {% endfor %}
    </div>
{% endblock %}

问题分析与解决方案

1. 核心问题:视图未传递通知列表到模板

当前notifications视图函数没有查询当前登录用户的通知列表,也未将其作为上下文变量传给模板。模板中使用的notifications变量可能来自全局上下文处理器,若处理器查询逻辑有疏漏,会导致渲染异常。

修复代码:
在视图的return render语句前添加通知查询逻辑,并将变量传入模板:

# 获取当前用户的所有通知(已在Model的Meta中设置按创建时间倒序)
notifications = request.user.notifications.all()
return render(request, 'notification/notifications.html', {'notifications': notifications})

2. 空白通知的排查与修复

前几项通知显示空白,说明通知对象存在,但模板中的if-elif未匹配到对应的notification_type,或关联的created_by用户不存在。

排查步骤:

  • 检查数据库中前几条通知的notification_type字段值,确认是否为message或application(注意与Model中定义的小写值匹配)
  • 查看这些通知的created_by用户是否被删除(硬删除或软删除)
  • 在模板中临时添加调试代码,查看未渲染通知的类型:
    {% for notification in notifications %}
        <div class="notification">
            <p>
                <!-- 调试:显示通知类型 -->
                通知类型: {{ notification.notification_type }}<br>
                {% if notification.notification_type == 'message' %}
                    <!-- 原代码 -->
                {% elif notification.notification_type == 'application' %}
                    <!-- 原代码 -->
                {% else %}
                    未知通知类型: {{ notification.notification_type }}
                {% endif %}
            </p>
        </div>
    {% endfor %}
    

对应修复:

  • 若notification_type值不匹配:修正数据库中的错误值,或在代码中统一处理大小写(比如添加|lower过滤器)
  • 若created_by用户不存在:在模板中添加默认值避免报错:
    <strong>{{ notification.created_by.username|default:"已删除用户" }}</strong>
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 17:20:25