Django【进阶篇-分页】

定义一个分页类,封装起来,保存到目录下面方便调用:pagination.py

Django【进阶篇-分页】_第1张图片

 

 具体完整代码:

from django.utils.safestring import mark_safe
#分页类
class Page(object):

    def __init__(self,current_page,data_count,per_page_count=10,page_num=11):
        """
            current_page:当前页
            data_count:数据量
            per_page_count:每页显示多少行数据
            page_num: 显示固定多少行页码
        """
        self.current_page = current_page
        self.data_count = data_count
        self.per_page_count = per_page_count
        self.page_num = page_num

    @property
    def start(self):
        """
            切片起始值
        """
        return (self.current_page-1) * self.per_page_count

    @property
    def end(self):
        """
            切片末尾值
        """
        return self.current_page * self.per_page_count
    @property
    def total_count(self):
        """
            总页码:v
            余数:y
            total_count返回的就是总页码值
        """
        v,y=divmod(self.data_count,self.per_page_count)
        if y:
            v +=1
        return v
    def page_str(self,base_url):
        """
            生成页码,上下页, 跳转等功能
            base_url:接收用户的url
        """
        self.base_url=base_url
        """
            page_list:页码列表
        """
        page_list = []
        if self.total_count < self.page_num:
            start_index=1
            end_index=self.total_count +1
        else:
            if self.current_page <= (self.page_num +1)/2:
                start_index = 1
                end_index = self.page_num +1
            else:
                start_index= self.current_page - (self.page_num -1)/2
                end_index= self.current_page +(self.page_num +1)/2
                if self.current_page + (self.page_num -1)/2 > self.total_count:
                    start_index = self.current_page -self.page_num +1
                    end_index = self.total_count +1
        # 上一页
        if self.current_page == 1:

            prev = '上一页'
        else:

            prev='上一页' %(self.base_url,self.current_page - 1)

        page_list.append(prev)
        # 循环的总页码
        for i in range(int(start_index),int(end_index)):
            if i == self.current_page:
                temp = '%s' % (self.base_url, i, i)
            else:
                temp= '%s ' % (self.base_url,i,i)
            page_list.append(temp)
        #下一页
        if self.current_page == self.total_count:
            next = '下一页'
        else:

            next = '下一页' % (self.base_url, self.current_page + 1)
        page_list.append(next)
        #跳转指定页码
        jump="""
            
            
            
        """ % (self.base_url)
        page_list.append(jump)
        #拼接字符串
        page_str= "".join(page_list)
        page_str=mark_safe(page_str)
        return page_str

views视图:

  —调用封装好的类

Django【进阶篇-分页】_第2张图片

 

 返回的page_str字符串到user_list.html界面进行渲染:

Django【进阶篇-分页】_第3张图片

 

jquery代码:

 Django【进阶篇-分页】_第4张图片

 

 

 tag:在页面通过include导入

Django【进阶篇-分页】_第5张图片

 

你可能感兴趣的:(Django【进阶篇-分页】)