Quantcast
Viewing all articles
Browse latest Browse all 14069

How to change the value of a field when the user clicked the notification button

I am developing a notification system in Django where users receive notifications when someone comments on an article they posted in the app. However, I am encountering an issue where I cannot modify the value of a field in the notification model when a user clicks a notification button in the template. Let me provide an example: When I ask a question on Stack Overflow and someone comments on the question, I receive a notification stating that someone has commented on my question. When I click the URL in the template, the red badge disappears, indicating that the author of the question has already read the comment on that question. This is precisely the functionality I am aiming to achieve here. However, using the current method in my views.py file, the method returns an error stating: '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>

Viewing all articles
Browse latest Browse all 14069

Trending Articles