if sender and receiver are not same in my Blog model then number of count not showing in my base.html. I am using signals in my models. Count is working for my blogcomment model but it’s not working in my blog model if sender and receiver are different. if I use sender = blog.author
and receiver = blog.author
then it working and counting. see the picture for get an idea:
here is my models.py #this model for author
class Blog(models.Model):
author = models.ForeignKey(User,on_delete=models.CASCADE,max_length=100)
title = models.CharField(max_length=300,unique=True)
#my others fields....
#here signals stating
def blog_notify(sender, instance, *args, **kwargs):
blog = instance
blog_title = blog.title
#sender =blog.author #only count working if sender and receiver is same
sender = User.objects.filter(username="admin1")
receiver = blog.author
if sender == blog.author and blog.is_published == "published":
notify = Notifications(blog=blog, sender=sender, receiver=receiver,text_preview = blog_title[:250], notification_type="post approved")
notify.save()
if sender == blog.author and blog.is_published == "pending":
notify = Notifications(blog=blog, sender=sender, receiver=receiver,text_preview = blog_title[:250], notification_type="pending post")
notify.save()
if sender == blog.author and blog.is_published == "rejected":
notify = Notifications(blog=blog, sender=sender,receiver=receiver,text_preview = blog_title[:250], notification_type="post rejected")
notify.save()
#this model for comment user. if anyone comment on blog post
class BlogComment(models.Model):
blog = models.ForeignKey(Blog,on_delete=models.CASCADE,null=True, blank=True)
#my others fields....
#here signals stating
def user_comment(sender, instance, *args, **kwargs):
comment= instance
blog = comment.blog
sender = comment.user
commnet_notes = comment.rejected_comment_notes
comment_text = comment.comment
if sender != blog.author and comment.is_published == "pending":
notify = Notifications(blog=blog, sender=sender, receiver=comment.blog.author,text_preview=comment_text[:250], notification_type="New Comment")
notify.save()
if sender != blog.author and comment.is_published == "published":
notify = Notifications(blog=blog, sender=sender, receiver=comment.blog.author,text_preview=comment_text[:250], notification_type="Comment Approved")
notify.save()
if sender != blog.author and comment.is_published == "rejected":
notify = Notifications(blog=blog, sender=sender, receiver=comment.blog.author,text_preview=comment_text[:250],help_notes=commnet_notes, notification_type="Comment Rejected")
notify.save()
post_save.connect(BlogComment.user_comment, sender=BlogComment)
post_save.connect(Blog.blog_notify, sender=Blog)
views.py which using for showing number of comment in my template
def Count_Notifications(request):
count_notifications_comment = 0
count_notifications_author = 0
blog_author = None
if request.user.is_authenticated:
count_notifications_comment = Notifications.objects.filter(sender=request.user,is_seen=False).count()
count_notifications_author = Notifications.objects.filter(receiver=request.user,is_seen_author_noti=False).count()
print('#########',count_notifications_author)
blog_author = Blog.objects.filter(author=request.user)
return {'count_notifications_comment':count_notifications_comment,'count_notifications_author':count_notifications_author,'blog_author':blog_author}
I am not understanding why count now working for Blog model if sender and receiver are different.