如何在 Django 中为上下文对象执行分页?

How to perform pagination for context object in django?

我在 views.py 中尝试过这样的事情:

class HomePage(TemplateView):
     template_name = "clouderp/index.html"

    def get_context_data(self, **kwargs):
        context = super(HomePage, self).get_context_data(**kwargs) 
        qs = Blog.objects.all()
        context['blog_list'] = qs

        page = self.request.GET.get('page')

        paginator = Paginator(qs, 4)

        try:
            users = paginator.page(page)
        except PageNotAnInteger:
            users = paginator.page(1)
        except EmptyPage:
            users = paginator.page(paginator.num_pages)

        context['users'] = users

        return context

在模板中:

{% if users.has_other_pages %}
<ul class="pagination">
  {% if users.has_previous %}
    <li><a href="?page={{ users.previous_page_number }}">&laquo;</a></li>
  {% else %}
    <li class="disabled"><span>&laquo;</span></li>
  {% endif %}
  {% for i in users.paginator.page_range %}
    {% if users.number == i %}
      <li class="active"><span>{{ i }} <span class="sr-only"></span></span></li>
    {% else %}
      <li><a href="?page={{ i }}">{{ i }}</a></li>
    {% endif %}
  {% endfor %}
  {% if users.has_next %}
    <li><a href="?page={{ users.next_page_number }}">&raquo;</a></li>
  {% else %}
    <li class="disabled"><span>&raquo;</span></li>
  {% endif %}
</ul>
{% endif %}

我已经使用 django 创建了一个博客应用程序
我想在我的主 index.html 页面中显示我所有的博客,并且还想在我的索引页面中对博客进行分页...
我想知道怎么做...
因为我做的过程博客不是按4分页的...

请尝试以下代码: 在 views.py 代码中

    class BookListView(ListView):
    model=Book
    paginate_by=10
    template_name='book/book_list.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        print (context)
        paginator = context['paginator']
        page_numbers_range = 10  # Display 5 page numbers
        max_index = len(paginator.page_range)

        page = self.request.GET.get('page')
        print (self.request)
        current_page = int(page) if page else 1

        start_index = int((current_page - 1) / page_numbers_range) * page_numbers_range
        end_index = start_index + page_numbers_range
        if end_index >= max_index:
            end_index = max_index

        page_range = paginator.page_range[start_index:end_index]
        context['page_range'] = page_range
        return context

book_list = BookListView.as_view()

和模板:

<div class="container">
            <!-- Pagination -->
            {% if is_paginated %}
            <nav>
                <ul class="pagination justify-content-center" style="margin:20px 0">
                {% if page_obj.has_previous %}
                    <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.previous_page_number }}">
                        <span>Prev</span>
                    </a>
                    </li>
                {% else %}
                    <li class="disabled page-item">
                    <a class="page-link" href="#">
                        <span>Prev</span>
                    </a>
                    </li>
                {% endif %}

                {% for page in page_range %}
                    <li {% if page == page_obj.number %} class="active page-item" {% endif %}>
                    <a class="page-link" href="?page={{ page }}">{{ page }}</a>
                    </li>
                {% endfor %}
                {% if page_obj.has_next %}
                    <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.next_page_number }}">
                        <span>Next</span>
                    </a>
                    </li>
                {% else %}
                    <li {% if not page_obj.has_next %}class="disabled page-item"{% endif %}>
                    <a class="page-link" href="#">
                        <span>Next</span>
                    </a>
                    </li>
                {% endif %}
                </ul>
            </nav>
            {% endif %}
    </div>