I have a notifications for a users when the admin add notification to user i want to display on user's home page like a facebook likes or activity feed is it possible with django ?
models.py
class Notification(BaseModel):
created_user = models.ForeignKey(User)
title = models.CharField(max_length=225)
description = models.TextField(max_length=600)
is_read = models.BooleanField(default=False)
priority = models.CharField(max_length=20, choices=NOTIFICATION_STATUS, default=LOW)
def __str__(self):
return self.title
i'm able to add a new notificiation but the notifications listing when the user refresh the page. Simply i just want to do this async like facebook like system.
Channels
is the best solution for this usecase. It's websocket and truly async and that connect with your frontend and backend sync in realtime.
base.html
socket = new WebSocket("ws://" + window.location.host + "/notification/");
socket.onmessage = function(e) {
notificationElement.innerHtml = e.data
}
models.py
@receiver(post_save, sender=Notification)
def notification_handler(sender, instance, **kwargs):
Group("notification").send({
"text": "This is the new notification",
})
来源:https://stackoverflow.com/questions/47392454/django-async-send-notifications