'method' 对象不支持项目分配

'method' object does not support item assignment

我正在尝试计算每个 post 的点赞次数,这是我的模型。但是我在项目详细信息页面上收到错误消息

likes = models.ManyToManyField(User, related_name='blog_posts')


    def total_likes(self):
        return self.likes.count()

查看:

class ItemDetailView(DetailView):
    model = Item
    template_name ='waqart/item_detail.html'

    
    def get_context_data(self, *args, **kwargs):
        context = super(ItemDetailView, self).get_context_data

        stuff = get_object_or_404(Item, id=self.kwargs['pk'])
        total_likes = stuff.total_likes()
        context['total_likes']= total_likes
        return context

您忘记打电话给 get_context_data。将其更改为:

context = super(ItemDetailView, self).get_context_data(*args, **kwargs)