Django 如何向基于ListView类的视图添加上下文

Django 如何向基于ListView类的视图添加上下文,django,django-views,Django,Django Views,我在修复与我的评论部分相关的小问题时遇到问题,我已为我的项目详细视图创建了一个评论部分,该部分工作正常,并显示了项目的评论数量。我有另一个列表视图,我想反映每个项目的评论数量 这是模型 class Comment(models.Model): STATUS = ( ('New', 'New'), ('True', 'True'), ('False', 'False'), ) item = models.ForeignKey(

我在修复与我的评论部分相关的小问题时遇到问题,我已为我的项目详细视图创建了一个评论部分,该部分工作正常,并显示了项目的评论数量。我有另一个列表视图,我想反映每个项目的评论数量

这是模型

class Comment(models.Model):
    STATUS = (
        ('New', 'New'),
        ('True', 'True'),
        ('False', 'False'),
    )
    item = models.ForeignKey(Item, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="ItemComments")
    comment = models.CharField(max_length=250, blank=True)
    status = models.CharField(max_length=10, choices=STATUS, default='New')
    create_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return '{} by {}'.format(self.subject, str(self.user.username))

    def total_comments(self):
        return self.comment.count()
这是item detail view.py

class ItemDetailView(DetailView):
    model = Item
    template_name = "product.html"

    def get_context_data(self, **kwargs):
        comments = Comment.objects.filter(item=self.object, status='New').order_by('-create_at')
        context = super().get_context_data(**kwargs)
        context["comments"] = comments
        context["total_comments"] = comments.count()
        return context
下面是我正在处理的列表视图,添加计数。我已经编写了代码,但它没有反映正确的计数,而是反映了所有项目的评论总数,我想分别显示每个项目的评论数

class DesignerOnlyPostListView(ListView):
    model = Item
    template_name = "designer_only_posts.html"
    context_object_name = 'items'
    paginate_by = 6

    def get_queryset(self):
        user = get_object_or_404(User, username=self.kwargs.get('username'))
        return Item.objects.filter(designer=user).order_by('-timestamp')

    def get_context_data(self, **kwargs):
        comments = Comment.objects.all()
        context = super().get_context_data(**kwargs)
        context["total_comments"] = comments.count()
        return context
这是模板

                    {% for item in items %}
                            {% if item.image %}
                                <a href="{{item.get_absolute_url}}">
                            {% endif %}
                                <h4><strong>{{item.title}}</strong></h4>
                                    <tr>
                                        <td>No. of Reviews:</td>
                                        <td>{{ total_comments }}</td>
                                    </tr>                                                                                                        
                    {% endfor %}
{items%]中的项的%
{%if item.image%}
{%endif%}
{{item.title}
覆核次数:
{{total_comments}}
{%endfor%}

在您的模型中,我会在项目属性中添加一个相关的名称

item = models.ForeignKey(Item, on_delete=models.CASCADE, related_name="comments")
这样,在你的评论中,你可以直接称之为

{% for item in items %}
        {% if item.image %}
              <a href="{{item.get_absolute_url}}">
        {% endif %}
        
         <h4><strong>{{item.title}}</strong></h4>
         <tr>
           <td>No. of Reviews:</td>
            <td>{{ item.comments.all.count }}</td>
         </tr>                                                                                                        
{% endfor %}
{items%]中的项的%
{%if item.image%}
{%endif%}
{{item.title}
覆核次数:
{{item.comments.all.count}
{%endfor%}