django 中的分页器

1:引入django内置的分页器

from django.core.paginator import Paginator

2:

    def get(self,request,page_num):
     “”“ page_num 指定页码 ”“”
    
        #模糊搜索
        q = self.get_search_query(["name", "start_time"])
        #查询用户

        if request.path_info == reverse("customer:customer",args=(page_num,)):
            all_customer =models.Customer.objects.filter(q,user__isnull = True)
        else:
            all_customer =models.Customer.objects.filter(q,user=request.user)

        #创建分页器 对象,all_customer要分页的对象,constants.CUSTOMER_LIST_PER_PAGE,每页的显示条数
        paginator = Paginator(all_customer,constants.CUSTOMER_LIST_PER_PAGE)
        #获取指定页码的数据
        page_customer = paginator.page(page_num)
        #获取总页数
        total_page = paginator.num_pages
        data = {
            'page_customer': page_customer,
            'page_num': page_num,
            'total_page': total_page,

        }
        return render(request,"customer.html",data)    

3:前端利用jQuery中的分页器

(1):引入jQuery分页器相关的包

<link rel="stylesheet" href="/static/css/jquery.pagination.css">
<script type="text/javascript" src="{{ static('js/jquery.pagination.min.js') }}"></script>

(2):准备分页器标签

<div class="r_wrap fr clearfix">
    <div class="pagenation">
        <div id="pagination" class="page"></div>
     </div>
 </div>

(3):准备分页器交互

<script type="text/javascript">
    $(function () {
        $('#pagination').pagination({
            currentPage: {{ page_num }},
            totalPage: {{ total_page }},
            callback:function (current) {
                // {#location.href = '/list/115/1/?sort=default';#}
                location.href = '/public/customerList/'+current;
            }
        })
    });
</script>

 

posted on 2020-10-31 22:48  paike123  阅读(301)  评论(0编辑  收藏  举报

导航