Django中封装分页组件

(1) 定义Paginator类

from django.utils.safestring import mark_safe


class Paginator(object):
    """
    初始化参数说明:
    request:请求对象
    data:查询到的符合条件的数据
    search_data:搜索框收索的关键字,解决收索情况下分页的BUG
    page_parm:分页请求的参数名称,例如:http://127.0.0.1:8000/phonenum/list/?page=2&q=
    page_size:每页显示的数据量
    plus:当前页分页按钮的两边显示的按钮数量
    """

    def __init__(self, request, data, search_data, page_parm="page", page_size=15, plus=5):

        page = request.GET.get(page_parm, "")
        if page.isdecimal():
            page = int(page)
        else:
            page = 1
        self.page = page
        self.page_size = page_size
        self.start = (page - 1) * page_size
        self.end = page * page_size
        self.data = data[self.start:self.end]
        total_count = data.count()
        total_page_count, div = divmod(total_count, page_size)
        if div:
            total_page_count += 1
        self.total_page_count = total_page_count
        self.plus = plus
        self.search_data = search_data
        self.page_parm = page_parm

    # 生成html标签
    def Html(self):
        if self.total_page_count <= 2 * self.plus + 1:
            start_page = 1
            end_page = self.total_page_count
        else:
            if self.page < self.plus:
                start_page = 1
                end_page = 2 * self.plus
            else:
                if (self.page + self.plus) > self.total_page_count:
                    start_page = self.total_page_count - 2 * self.plus
                    end_page = self.total_page_count
                else:
                    start_page = self.page - self.plus
                    end_page = self.page + self.plus
        # 分页按钮
        page_str_list = []
        # 首页
        first_page = '<li><a href="?{}={}&q={}">首页</a></li>'.format(self.page_parm, 1, self.search_data)
        page_str_list.append(first_page)
        # 上一页
        if self.page > 1:
            pre = '<li><a href="?{}={}&q={}">上一页</a></li>'.format(self.page_parm, self.page - 1, self.search_data)
        else:
            pre = '<li><a href="?{}={}&q={}">上一页</a></li>'.format(self.page_parm, 1, self.search_data)
        page_str_list.append(pre)

        for i in range(start_page, end_page + 1):
            if i == self.page:
                ele = '<li class="active"><a href="?{}={}&q={}">{}</a></li>'.format(self.page_parm, i, self.search_data,
                                                                                    i)
            else:
                ele = '<li><a href="?{}={}&q={}">{}</a></li>'.format(self.page_parm, i, self.search_data, i)
            page_str_list.append(ele)
        # 下一页
        if self.page < self.total_page_count:
            next = '<li><a href="?{}={}&q={}">下一页</a></li>'.format(self.page_parm, self.page + 1, self.search_data)
        else:
            next = '<li><a href="?{}={}&q={}">下一页</a></li>'.format(self.page_parm, self.total_page_count,
                                                                   self.search_data)
        page_str_list.append(next)
        # 尾页
        last_page = '<li><a href="?{}={}&q={}">尾页</a></li>'.format(self.page_parm, self.total_page_count,
                                                                   self.search_data)
        page_str_list.append(last_page)
        search_string = """
                        <div style="width: 150px;margin-bottom: 20px;display: inline-block">
                            <form method="get">
                                <div class="input-group">
                                    <input type="text" class="form-control" placeholder="请输入页码" name="{}">
                                    <span class="input-group-btn">
                                        <button class="btn btn-primary" type="submit">跳转</button>
                                    </span>
                                </div>
                            </form>
                        </div>
                        """.format(self.page_parm)
        page_str_list.append(search_string)
        page_string = mark_safe("".join(page_str_list))
        return page_string

(2) 导入Paginator类,传入参数如下所示

 def phoneNum_list(request):
    """靓号列表"""
    data_dict={}
    search_data=request.GET.get("q","")
    if search_data:
        data_dict["phone_num__contains"]=search_data
    # 分页
    data = PrettyPhoneNum.objects.filter(**data_dict).order_by("level")
    page_object=Paginator(request,data,search_data) # 其他三个参数根据需求调整
    context={
        "data": page_object.data,
        "search_data": search_data,
        "page_string": page_object.Html()
    }
    return render(request,"phonenum_list.html",context)

data为查询到的分页后的数据,search_data为收索的关键字,返回用于搜索框保留上次搜索的内容,page_string为分页按钮

(3) 在前端显示分页按钮

<nav aria-label="...">
     <ul class="pagination">
          {{ page_string }}
     </ul>
</nav>