I am building an notifcation system. I am almost complete the notifcation system. Now my problem is notifcation content not rendering my html template. I am not understanding where I am doing mistake.here is my code:
notifications models.py:
class Notifications(models.Model):
blog = models.ForeignKey('blog.Blog',on_delete=models.CASCADE)
NOTIFICATION_TYPES = (('New Comment','New Comment'),('Comment Approved','Comment Approved'), ('Comment Rejected','Comment Rejected'))
sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name="noti_from_user")
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="noti_to_user")
notification_type = models.CharField(choices=NOTIFICATION_TYPES,max_length=250)
text_preview = models.CharField(max_length=500, blank=True)
date = models.DateTimeField(auto_now_add=True)
is_seen = models.BooleanField(default=False)
views.py
def ShowNOtifications(request):
user = request.user
notifications = Notifications.objects.filter(user=user).order_by('-date')
Notifications.objects.filter(user=user, is_seen=False).update(is_seen=True)
template_name ='blog/notifications.html'
context = {
'notify': notifications,
}
return render(request,template_name,context)
#html
{% for notification in notifications %}
{% if notification.notification_type == "New Comment" %}
@{{ notification.sender.username }}You have received new commnet
<p>{{ notification.text_preview }}</p>
{%endif%}
{%endfor%}
why notification content not showing in my html template? where I am doing mistake?
'notify', so in your template you need usenotify.sender.usernameinstead ofnotification.sender.username