单个用户的Django通知系统
我想创建一个通知系统 我创建了一个徽章,该徽章指示我的页面上的通知,以查看各种通知,并通过使用GET方法的Ajax更新通知的数量。 计数不在单个用户上,因此仍然是通知的数量,我希望单击“读取”按钮的用户不再看到通知,并且会员会减少。 但是我该如何为单个用户做,我可以做到这一点,但它对每个用户而不是单个用户都有效,仅此而已。
模型
class Notification(models.Model):
date = models.DateField(auto_now_add = True)
title = models.CharField(max_length = 255)
text = models.TextField()
active = models.BooleanField(default = False)
查看通知 +查看AJAX通知
def notifiche(request):
if request.is_ajax() and request.method == "POST":
notifica = request.POST['id_notifica']
oggetto = get_object_or_404(Notification, id = notifica)
if oggetto.active == True:
oggetto.active = False
messaggio = "Notifica disattivata"
else:
oggetto.active = True
messaggio = "Notifica attiva sul sito"
oggetto.save()
return JsonResponse({'oggetto': oggetto.active, 'messaggio': messaggio}, status=200)
else:
notifiche = Notification.objects.all()
context = {'notifiche':notifiche, 'popup':popup}
return render(request, 'notifiche.html', context)
def popup(request):
popup = Notification.objects.filter(active = True).count()
return JsonResponse({'popup': popup}, status=200)
片段/nav.html
<nav class="navbar navbar-expand-lg bg-dark">
<div class="container">
<div class="collapse navbar-collapse justify-content-end">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link text-light" aria-current="page" href="{% url 'notifiche' %}">
Notifiche <span class="badge bg-danger">0</span>
</a>
</li>
</ul>
</div>
</div>
</nav>
base.html中的ajax代码
<script>
$.ajax({
url: "/ajax/notifiche/",
type: "GET",
dataType: 'json',
success: function(response){
$("nav span.badge").text(response.popup);
}
});
</script>
I would like to create a notification system
I created the badge that signals me the notification on its page to see the various notifications and through ajax with the GET method I update the number of notifications.
Not being attached to a single user, the count remains the number of notifications and I would like that user who clicks the 'read' button the notification is no longer seen and the conuter decreases.
But how do I do it for the single user, I can do it but it would work for each user and not for the single one and that's it.
model
class Notification(models.Model):
date = models.DateField(auto_now_add = True)
title = models.CharField(max_length = 255)
text = models.TextField()
active = models.BooleanField(default = False)
View notification + view ajax notification
def notifiche(request):
if request.is_ajax() and request.method == "POST":
notifica = request.POST['id_notifica']
oggetto = get_object_or_404(Notification, id = notifica)
if oggetto.active == True:
oggetto.active = False
messaggio = "Notifica disattivata"
else:
oggetto.active = True
messaggio = "Notifica attiva sul sito"
oggetto.save()
return JsonResponse({'oggetto': oggetto.active, 'messaggio': messaggio}, status=200)
else:
notifiche = Notification.objects.all()
context = {'notifiche':notifiche, 'popup':popup}
return render(request, 'notifiche.html', context)
def popup(request):
popup = Notification.objects.filter(active = True).count()
return JsonResponse({'popup': popup}, status=200)
Snippets/nav.html
<nav class="navbar navbar-expand-lg bg-dark">
<div class="container">
<div class="collapse navbar-collapse justify-content-end">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link text-light" aria-current="page" href="{% url 'notifiche' %}">
Notifiche <span class="badge bg-danger">0</span>
</a>
</li>
</ul>
</div>
</div>
</nav>
Ajax code in base.html
<script>
$.ajax({
url: "/ajax/notifiche/",
type: "GET",
dataType: 'json',
success: function(response){
$("nav span.badge").text(response.popup);
}
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过多种方式执行此操作:
如果每个收到通知的用户都有一个帐户名已注册,请添加一个用户字段,其中包含Checkbox或Textfield,并带有split(“ \ n”)
的复选框或textfield,您也可以在本地使用cookie或数据,并且如果已经在显示一次(可以在JS中100%完成,没有帐户系统)
情况下完成),则不会显示通知。
You can do it by many ways :
If every users receiving the notification have an account name registered, add a users field with checkbox or TextField with a split("\n")
You can also use cookies or data stored locally and don't display the notification if already shown once (can be done 100% in js and without an account system)
Probably more ways...