Django-website 程序案例系列-9 分页

  

 分页例子程序:

LIST = []    #全局列表
for i in range(103):  #1:100的列表
    LIST.append(i)
def user_list(request):
    current_page = request.GET.get('p', 1)  # GET接收的都是字符串
    current_page = int(current_page)    #字符串转换成数字

    per_page_count = 5          #每页显示多少条数据
    start = (current_page - 1) * per_page_count       #页数显示数据开始
    end = current_page * per_page_count              #页数显示数据接收
    data = LIST[start:end]              #生成显示列表的索引

    all_count = len(LIST)               #判断列表总长度
    total_count, y = divmod(all_count, per_page_count )       #取模函数,取10的模,count为取模多少次,y是剩余多少
    if y:                           #如果y不为0 说明还有剩余的数
        total_count += 1

    page_list = []              #建立一个空列表
    pageer_num = 7             #显示页码数
    start_index = current_page - 5   #开始显示的页码数
    end_index = current_page + 5 + 1  #结束显示的页码数
    if total_count < pageer_num:  #
        start_index = 1
        end_index = total_count + 1
    else:
        if current_page <= (pageer_num + 1)/2:
            start_index = 1
            end_index = pageer_num + 1
        else:
            start_index = current_page - (pageer_num - 1)/2
            end_index = current_page + (pageer_num + 1)/2
            if (current_page + (pageer_num - 1)/2) > total_count:
                end_index = total_count + 1
                start_index = total_count - pageer_num + 1

    if current_page == 1:   #当页数为开始时
        prev = '<a class="page" href="javascript:void(0);">上一页</a>' #添加上一页标签不做跳转
    else:
        prev = '<a class="page" href="/user_list/?p=%s">上一页</a>' % (current_page - 1) #否则添加带跳转参数的标签
    page_list.append(prev)
    for i in range(int(start_index), int(end_index)):     #显示的页数是取模的数
        if i == current_page:       #如果判断页数为当前显示的页数
            temp = '<a class="page active" href="/user_list/?p=%s">%s</a>' % (i, i) #给列表中传这个字符串,class中加入active的css样式
        else:
            temp = '<a class="page" href="/user_list/?p=%s">%s</a>' % (i, i) #不是当前显示就传这个字符串
        page_list.append(temp)      #将字符串压入列表

    if current_page == total_count:  #当页数显示到尽头时
        nex = '<a class="page" href="javascript:void(0);">下一页</a>'   #添加下一页标签不做跳转
    else:
        nex = '<a class="page" href="/user_list/?p=%s">下一页</a>' % (current_page + 1)  #否则添加带跳转参数的标签
    page_list.append(nex)
    #以下为添加跳转框的js代码
    jump = """
    <input type='text' /><a onclick='jumpTo(this, "/user_list/?p=")'>Go</a>
    <script>
            function jumpTo(th, base){    
                var val = th.previousSibling.value;  
                location.href = base + val;     
            }
    </script>
    """
    page_list.append(jump)
    page_str = mark_safe("".join(page_list))    #转换成字符串,并打上安全标记传都前端
    return render(request, 'user_list.html', {'li': data, 'page_str': page_str})

 

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .pagination .page{
            display: inline-block;
            padding: 5px;
            background-color: cornflowerblue;
            margin: 10px;
        }
        .pagination .page.active{
            background-color: #00aa00;
            color: white;
        }
    </style>
</head>
<body>
    <ul>
        {% for item in li %}
            {% include 'li.html' %}
        {% endfor %}
    </ul>
    <div class="pagination">
        {{ page_str }}
    </div>
</body>
</html>

  

封装成分页类:

class Page:

    def __init__(self, current_page, data_count, per_page_num=10, pager_num=7):
        self.current_page = current_page
        self.data_count = data_count
        self.per_page_num = per_page_num
        self.pager_num = pager_num

    @property    #调用该方法时不用加括号 如:x.start() 直接使用 x.start 就可以调用
    def start(self):
        return (self.current_page - 1) * self.per_page_num

    @property
    def end(self):
        return self.current_page * self.per_page_num

    @property
    def total_count(self):
        v, y = divmod(self.data_count, self.per_page_num)
        if y:
            v += 1
        return v

    def page_str(self, base_url):

        page_list = []

        if self.total_count < self.pager_num:
            start_index = 1
            end_index = self.total_count + 1
        else:
            if self.current_page <= (self.pager_num + 1) / 2:
                start_index = 1
                end_index = self.pager_num + 1
            else:
                start_index = self.current_page - (self.pager_num - 1) / 2
                end_index = self.current_page + (self.pager_num + 1) / 2
                if (self.current_page + (self.pager_num - 1) / 2) > self.total_count:
                    end_index = self.total_count + 1
                    start_index = self.total_count - self.pager_num + 1

        if self.current_page == 1:
            prev = '<a class="page" href="javascript:void(0);">上一页</a>'
        else:
            prev = '<a class="page" href="%s?p=%s">上一页</a>' % (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 = '<a class="page active" href="%s?p=%s">%s</a>' % (base_url, i, i)
            else:
                temp = '<a class="page" href="%s?p=%s">%s</a>' % (base_url, i, i)
            page_list.append(temp)

        if self.current_page == self.total_count:
            nex = '<a class="page" href="javascript:void(0);">下一页</a>'
        else:
            nex = '<a class="page" href="%s?p=%s">下一页</a>' % (base_url, self.current_page + 1)
        page_list.append(nex)

        jump = """
           <input type='text' /><a onclick='jumpTo(this, "%s?p=")'>Go</a>
           <script>
                   function jumpTo(th, base){    
                       var val = th.previousSibling.value;  
                       location.href = base + val;     
                   }
           </script>
           """ % (base_url)
        page_list.append(jump)
        page_str = mark_safe("".join(page_list))

        return page_str

  

调用分页类:

LIST = []    #全局列表
for i in range(103):  #1:100的列表
    LIST.append(i)
def user_list(request):
    current_page = request.GET.get('p', 1)
    current_page = int(current_page)

    page_obj = Page(current_page, len(LIST))

    data = LIST[page_obj.start: page_obj.end]

    page_str = page_obj.page_str("/user_list/")
    return render(request, 'user_list.html', {'li': data, 'page_str': page_str})

  

 可以直接放到一个文件夹中,作为工具来调用:

调用方法为:

from utils import pagination

obj = pagination.Page(xx,yy)  #创建分页类

posted @ 2017-11-27 21:24  划得戳  阅读(224)  评论(0编辑  收藏  举报