【问题标题】:How to show page items count in django pagination for CBV?如何在 CBV 的 django 分页中显示页面项目数?
【发布时间】:2019-03-10 14:25:19
【问题描述】:

我试图弄清楚如何在我的模板中使用 django 分页来显示类似“显示 52 个中的 1-10 个”的内容。

我在我的应用程序中使用 CBV。

这就是我所做的

在我看来.py:

class viewbloglistview(LoginRequiredMixin,ListView):
    model = Blog
    paginate_by = 6

在我的模板中:

{% if is_paginated %}
      <ul class="pagination">
       {% if page_obj.has_previous %}
           <li><a href="?page={{ page_obj.previous_page_number }}">Previous</a></li>
       {% else %}
            <li class="disabled"><span>Previous</span></li>
       {% endif %}
       {% for i in paginator.page_range %}
         {% if page_obj.number == i %}
             <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
         {% else %}
              <li><a href="?page={{ i }}">{{ i }}</a></li>
         {% endif %}
       {% endfor %}
          {% if page_obj.has_next %}
             <li><a href="?page={{ page_obj.next_page_number }}">Next</a></li>
          {% else %}
               <li class="disabled"><span>Next</span></li>
       {% endif %}
      </ul>
{% endif %}

有什么想法可以在 django 中实现吗?

谢谢

【问题讨论】:

    标签: django django-templates django-views django-pagination


    【解决方案1】:

    您可以在您的views.py中获取查询集的计数并添加到您现有的上下文中:

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super().get_context_data(**kwargs)
        context['total'] = self.get_queryset().count
        return context
    

    【讨论】:

      猜你喜欢
      • 2010-12-13
      • 2019-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      • 2020-10-28
      • 1970-01-01
      • 2021-09-26
      相关资源
      最近更新 更多