如何在Django中对上下文对象执行分页? [英] How to perform pagination for context object in django?

查看:88
本文介绍了如何在Django中对上下文对象执行分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在views.py中尝试过类似的方法:

I have tried something like this in 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分页...

I have created a blog application using django
I want to show all my blogs in my main index.html page and also wanted to do pagination for the blogs in my index page...
I was wondering how to do it...
Because the process I have done the blogs are not paginated according to 4 at a time...

推荐答案

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

Please try this code as below: in views.py code

    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>

这篇关于如何在Django中对上下文对象执行分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆