python - 如果发送者和接收者不相同,Django Count 不能在信号中工作
问题描述
如果我的博客模型中的发送者和接收者不相同,则我的 base.html 中未显示计数。我在我的模型中使用信号。Count 适用于我的 blogcomment 模型,但 如果发送者和接收者不同,它不适用于我的博客模型。如果我使用sender = blog.author
然后 receiver = blog.author
它工作和计数。看图片了解一下:
这是我的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用于在我的模板中显示评论数量
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}
如果发送者和接收者不同,我不明白为什么 count 现在适用于博客模型。
解决方案
推荐阅读
- php - 从 WooCommerce 结帐通知中更改“未选择运输方式...”
- ruby-on-rails - 您如何将格式错误的字符串转换为日期时间
- node.js - 如何根据一个标准按 Mongo 对文档进行排序?
- c# - 它不会将我重定向到控制器的索引
- excel - 使用 VBA 将公式插入特定单元格
- php - 使用 CURL 上传给定的文件和 POST-Date
- twilio - 在 Twilio 中终止 Pexip
- python - 如何在 Scatter3D 中绘制超平面?
- java - 如何通过在 android 中包含布局从线性布局调用 textview?
- linux - 如何在 Buildroot 中为 Moxa UPort 1110 USB 串口转换器配置驱动程序?