Creating simple notification page using django.
I have tried many different code but none of the concepts worked for my project. whenever we click on the notification page notification like post created at a particular time should get displayed.
here is my views.py
def notification(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('view_application',
application_id=notification.extra_id)
elif notification.notification_type == Notification.APPLICATION:
return redirect('view_application',
application_id=notification.extra_id)
return render(request, 'web/notification.html')
here is my models.py
class Notification(models.Model):
MESSAGE = 'message'
APPLICATION = 'application'
CHOICES = (
(MESSAGE, 'Message'),
(APPLICATION, 'Application')
)
to_member = models.ForeignKey(Member, related_name='notification', 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(Member, related_name='creatednotifications', on_delete=models.CASCADE)
class Meta:
ordering = ['created_at']
def str(self): self:to_member
here is my template notification.html
{% if not notification %}
No notifications yet!
{% endif %}
{% for notification in notification %}
<div class="notification">
<p>
{% if notification.notification_type == 'message' %}
<a href="{% url 'notification' %}?goto=view_application¬ification={{ 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 'notification' %}?goto=view_application¬ification={{ notification.id }}&extra_id={{ notification.extra_id }}">
<strong>{{ notification.created_by.username }}</strong> applied for your job<br>
<small>{{ notification.created_at|timesince }} ago</small>
</a>
{% endif %}
all the notification should appear here