如何在用户点击通知按钮时更改字段值

0 投票
1 回答
27 浏览
提问于 2025-04-13 02:52

我正在用Django开发一个通知系统,用户在应用里发布文章后,如果有人评论,就会收到通知。不过,我遇到了一个问题,就是当用户在模板中点击通知按钮时,无法修改通知模型中的某个字段的值。让我举个例子:当我在Stack Overflow上提问,有人对我的问题评论时,我会收到一条通知,告诉我有人评论了我的问题。当我点击模板中的链接时,红色的徽章就会消失,这表示提问的作者已经看过这个评论。这正是我想要实现的功能。不过,在我当前的views.py文件中使用的方法时,出现了一个错误,提示:'int' object has no attribute 'is_read'.

def all_article(request):
    articles = Article.objects.all()
    notifications = Notification.objects.filter(user=request.user)

    unread_notifications = Notification.objects.filter(user=request.user, is_read=False).count()

    if unread_notifications.is_read == False:
        unread_notifications.is_read == True
        unread_notifications.save()
    return render(request, 'article.html', {"articles":articles, "notifications":notifications, "unread_notifications":unread_notifications})

models.py:

class Notification(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    message = models.TextField()
    link = models.URLField(unique=True)
    is_read = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return str(self.user)

template:

<nav class="navbar navbar-light bg-light shadow-sm bg-body">
        <div class="container">
            <a class="navbar-brand" href="">
                <h5>Ex-Muslims of Northern Nigeria</h5>
            </a>
            <ul class="navbar-nav ms-auto flex-row">
                <li class="nav-item">
                    <div class="offcanvas offcanvas-top" tabindex="-1" id="offcanvasTop" aria-labelledby="offcanvasTopLabel">
                    <div class="offcanvas-header">
                        <h5 id="offcanvasTopLabel">Notifications</h5>
                        <button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
                    </div>

                    <div class="offcanvas-body">
                        {% for notification in notifications reversed %}
                        <ul class="list-group my-2">
                            <a href="{{ notification.link }}">
                                <li class="list-group-item">
                                    {{ notification.message }}
                                </li>
                            </a>
                          </ul>
                          {% endfor %}
                    </div>
                    </div>

                </li>

            </ul>

            <ul class="navbar-nav ms-auto flex-row">

                <li class="nav-item me-3">
                    <a class="btn btn-light shadow-sm bg-body" href="{% url 'Post' %}">
                    Add
                </a>
                </li>

                <li class="nav-item me-3">
                    <a class="btn btn-light shadow-sm bg-body position-relative" href="" 
                    type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasTop" 
                    aria-controls="offcanvasTop">
                    Notifications
                        {% if unread_notifications %}
                        <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger">
                            {{unread_notifications}}
                        </span>
                        {% endif %}
                    </a>
                </li>

                <li class="nav-item">
                    <a class="btn btn-light shadow-sm bg-body" href="{% url 'Profile' %}">
                    Profile
                </a>
                </li>

            </ul>

            
            
        </div>
    </nav>

1 个回答

0

在你的视图中,你有

unread_notifications = 
Notification.objects.filter(user=request.user, is_read=False).count()

在最后加上count(),如果有1个未读通知,unread_notifications的值就会是1。它不会是未读通知的具体对象,所以它就没有is_read这个属性。

我猜你想要的是:

unread_notifications = Notification.objects.filter(user=request.user, is_read=False)

for notification in unread_notifications:
    #We don't need this line as the filter guarantees is_read=False
    #if notification.is_read == False:
    notification.is_read = True
    notification .save()

撰写回答