Python 计算用户收到的所有帖子的赞数-Django

Python 计算用户收到的所有帖子的赞数-Django,python,django,Python,Django,我正试图得到一个用户喜欢的总数,在我的例子中,这个用户就是这篇文章的作者 我已经评论了我的审判,因为它不起作用 这是模型 这是我尝试过的views.py 以下是模板: 更新: 为了解决这个问题,我添加了: class PostDetailView(DetailView): model = Post template_name = "blog/post_detail.html" # <app>/<model>_<viewtype&

我正试图得到一个用户喜欢的总数,在我的例子中,这个用户就是这篇文章的作者

我已经评论了我的审判,因为它不起作用

这是模型

这是我尝试过的views.py

以下是模板:

更新: 为了解决这个问题,我添加了:

class PostDetailView(DetailView):
    model = Post
    template_name = "blog/post_detail.html"  # <app>/<model>_<viewtype>.html

    def get_context_data(self, *args, **kwargs):
        context = super(PostDetailView, self).get_context_data()
        total_likes_received = Post.likes.filter(author=self.request.user).count()
What should I add here to show the total likes of all posts of an author not the logged in user
        context['total_likes_received'] = total_likes_received


我的问题是:
如何获得某位作者所有帖子的赞总数我想你可以使用下面的查询来实现这一点

all_posts = request.user.author.all()
total_likes_received = all_posts.aggregate(total_likes=Count('likes'))['total_likes']

我认为您可以使用以下查询来完成此操作

all_posts = request.user.author.all()
total_likes_received = all_posts.aggregate(total_likes=Count('likes'))['total_likes']

@问题是您使用的是DetailView,您的代码很好,但使用得不好 答案很简单,在不更改代码的情况下,您可以这样做:

class PostDetailView(DetailView):
    model = Post
    template_name = "blog/post_detail.html"

    def total_likes_received(self):
        total_likes_received = Like.objects.filter(post__author=post.author).count()
        return total_likes_received

    def get_context_data(self, **kwargs):
        """Insert the single object into the context dict."""
        context = super(PostDetailView, self).get_context_data(**kwargs)
        context['total_likes_received'] = self.total_likes_received()

        return context
total_likes_received = Like.objects.filter(post__author=self.request.user).count()

现在,在模板中,您不必更改任何内容。

@A_K问题是您使用的是DetailView,您的代码很好,但使用得很糟糕 答案很简单,在不更改代码的情况下,您可以这样做:

class PostDetailView(DetailView):
    model = Post
    template_name = "blog/post_detail.html"

    def total_likes_received(self):
        total_likes_received = Like.objects.filter(post__author=post.author).count()
        return total_likes_received

    def get_context_data(self, **kwargs):
        """Insert the single object into the context dict."""
        context = super(PostDetailView, self).get_context_data(**kwargs)
        context['total_likes_received'] = self.total_likes_received()

        return context
total_likes_received = Like.objects.filter(post__author=self.request.user).count()

现在,在您的模板中,您无需更改任何内容。

您可以像这样简单地查询:

class PostDetailView(DetailView):
    model = Post
    template_name = "blog/post_detail.html"

    def total_likes_received(self):
        total_likes_received = Like.objects.filter(post__author=post.author).count()
        return total_likes_received

    def get_context_data(self, **kwargs):
        """Insert the single object into the context dict."""
        context = super(PostDetailView, self).get_context_data(**kwargs)
        context['total_likes_received'] = self.total_likes_received()

        return context
total_likes_received = Like.objects.filter(post__author=self.request.user).count()

您可以这样简单地查询:

class PostDetailView(DetailView):
    model = Post
    template_name = "blog/post_detail.html"

    def total_likes_received(self):
        total_likes_received = Like.objects.filter(post__author=post.author).count()
        return total_likes_received

    def get_context_data(self, **kwargs):
        """Insert the single object into the context dict."""
        context = super(PostDetailView, self).get_context_data(**kwargs)
        context['total_likes_received'] = self.total_likes_received()

        return context
total_likes_received = Like.objects.filter(post__author=self.request.user).count()

你可以做以下事情来获得这篇文章

post = get_object_or_404(Post, pk=self.kwargs['pk']) 
total_likes_received = Like.objects.filter(post__author=post.author).count()

你可以做以下事情来获得这篇文章

post = get_object_or_404(Post, pk=self.kwargs['pk']) 
total_likes_received = Like.objects.filter(post__author=post.author).count()


那它怎么办?它不在模板中显示。我会将模板添加到您需要将Post.like.filter更改为Post.objects的问题中。filter@Omar这将计算帖子的数量,而不是喜欢。你应该有另一个类似的模型,对吗?比如.objects.filterauthor=request.user.count。它怎么办?它没有显示在模板中。我会将模板添加到您需要将Post.like.filter更改为Post.objects的问题中。filter@Omar这将计算帖子的数量,而不是喜欢。你应该有另一个类似的模型,对吗?Like.objects.filterauther=request.user.count。我添加了total\u likes\u received函数,但它返回的“ManyToManyDescriptor”对象没有属性“filter”确定一分钟后我更新total\u likes\u received函数以使您恢复正常确定刷新页面我显示我的新版本并重试it@A_K现在可以了吗?实际上这是返回作者添加了total_likes_received函数,但它返回了'ManyToManyDescriptor'对象没有属性'filter'确定一分钟后我更新total_likes_received函数以使您恢复正常确定刷新页面我显示我的新版本并重试it@A_K现在可以了吗?实际上这是返回一个作者试图添加此代码的帖子总数但我得到的名称“请求”未定义我已将其添加到我的get_context_数据中它工作正常,但它显示的是登录用户而不是帖子详细信息作者收到的总喜欢,因为我使用的是def get_context_dataself,*args,**kwargs:我只是想给你一个想法,您需要根据需要集成其余部分:我尝试添加此代码,但我得到的名称“request”未定义我已将其添加到我的get_context_数据中它工作正常,但它显示的是登录用户而不是帖子详细信息作者收到的总喜欢,因为我使用的是def get_context_dataself,*args,**kwargs:我只是想给你一个想法,你需要根据你的需要整合其余部分:我已将get_context_数据添加到我的get_context_数据中,它工作正常,但它显示的是登录用户而不是帖子详细信息作者收到的总喜好,因为我使用的是def get_context_dataself,*args,**kwargs:我已将get_context_数据添加到我的get_context_数据中,它工作正常,但它显示的是登录用户而不是帖子详细信息作者收到的总喜好,因为我使用的是def get_context_dataself,*args,**kwargs: