django-分页

一 创建分页视图类
from .models import Data
from django.views.generic import ListView
class DataListView(ListView):
    model = Data
    # 渲染的页面
    template_name = 'index.html'
    # 上下文名字
    context_object_name = 'datas'
    # 每一页展示多少条数据
    paginate_by = 20
    ordering = 'code'
    page_kwarg = 'p'

    # 获取上下文处理器
    def get_context_data(self, **kwargs):
        context = super(DataListView, self).get_context_data(*kwargs)
        paginator = context.get('paginator')
        page_obj = context.get('page_obj')
        pagination_data = self.get_pagintion_data(paginator, page_obj)
        context.update(pagination_data)
        return context

    def get_pagintion_data(self, paginator, page_obj, around_count=2):
        current_page = page_obj.number
        num_pages = paginator.num_pages

        # 显示...
        left_has_more = False
        right_has_more = False

        if current_page <= around_count + 2:
            left_pages = range(1, current_page)
        else:
            left_has_more = True
            left_pages = range(current_page - around_count, current_page)

        if current_page >= num_pages - around_count - 1:
            right_pages = range(current_page + 1, num_pages + 1)
        else:
            right_has_more = True
            right_pages = range(current_page + 1, current_page + around_count + 1)

        return {
            'left_pages': left_pages,
            'right_pages': right_pages,
            'current_page': current_page,
            'left_has_more': left_has_more,
            'right_has_more': right_has_more,
            'num_pages': num_pages
        }

二 配置url映射

path('list/', views.DataListView.as_view(), name="list")

三 渲染前端页面

<ul class="pagination pagination-sm">
                    {#是否还有上一页#}
                    {% if page_obj.has_previous %}
                        <li><a href="{% url 'data:list' %}?p={{ page_obj.previous_page_number }}">上一页</a></li>
                    {% else %}
                        <li class="disabled"><a href="javascript:void(0)">上一页</a></li>
                    {% endif %}


                    {# 中间的页码 #}
                    {# 是否显示左边... #}
                    {% if left_has_more %}
                        <li><a href="{% url 'data:list' %}?p=1">首页</a></li>
                        <li><a href="javascript:void(0)">...</a></li>
                    {% endif %}

                    {# 左边的代码 #}
                    {% for left_page in left_pages %}
                        <li><a href="{% url 'data:list' %}?p={{ left_page }}">{{ left_page }}</a></li>
                    {% endfor %}

                    {# 当前的页码 #}
                    <li class="active"><a href="{% url 'data:list' %}?p={{ current_page }}">{{ current_page }}</a></li>

                    {# 右边的页码 #}
                    {% for right_page in right_pages %}
                        <li><a href="{% url 'data:list' %}?p={{ right_page }}">{{ right_page }}</a></li>
                    {% endfor %}

                    {# 是否显示右边... #}
                    {% if right_has_more %}
                        <li><a href="javascript:void(0)">...</a></li>
                        <li><a href="{% url 'data:list' %}?p={{ num_pages }}">尾页</a></li>
                    {% endif %}



                    {# 下一页 #}
                    {% if page_obj.has_next %}
                        <li><a href="{% url 'data:list' %}?p={{ page_obj.next_page_number }}">下一页</a></li>
                    {% else %}
                        <li class="disabled"><a href="javascript:void(0)">下一页</a></li>
                    {% endif %}

                </ul>

 

 

posted @ 2019-05-16 14:14  菜鸟SSS  阅读(259)  评论(0编辑  收藏  举报