自定义分页

创建一个py文件:(mypage)

class Pagination(object):

    def __init__(self, data_num, current_page, url_prefix, per_page=10,  max_show=11):
        """
        进行初始化.
        :param data_num: 数据总数
        :param current_page: 当前页
        :param url_prefix: 生成的页码的链接前缀
        :param per_page: 每页显示多少条数据
        :param max_show: 页面最多显示多少个页码
        """
        self.data_num = data_num
        self.per_page = per_page
        self.max_show = max_show
        self.url_prefix = url_prefix

        # 把页码数算出来
        self.page_num, more = divmod(data_num, per_page)
        if more:
            self.page_num += 1

        try:
            self.current_page = int(current_page)
        except Exception as e:
            self.current_page = 1
            # 如果URL传过来的页码数是负数
        if self.current_page <= 0:
            self.current_page = 1
            # 如果URL传过来的页码数超过了最大页码数
        elif self.current_page > self.page_num:
            self.current_page = self.page_num  # 默认展示最后一页

        # 页码数的一半 算出来
        self.half_show = max_show // 2

        # 页码最左边显示多少
        if self.current_page - self.half_show <= 1:
            self.page_start = 1
            self.page_end = self.max_show
        elif self.current_page + self.half_show >= self.page_num:  # 如果右边越界
            self.page_end = self.page_num
            self.page_start = self.page_num - self.max_show
        else:
            self.page_start = self.current_page - self.half_show
            # 页码最右边显示
            self.page_end = self.current_page + self.half_show


    @property
    def start(self):
        # 数据从哪儿开始切
        return (self.current_page - 1) * self.per_page

    @property
    def end(self):
        # 数据切片切到哪儿
        return self.current_page * self.per_page

    def page_html(self):
        # 生成页码
        l = []
        # 加一个首页
        l.append('<li><a href="{}?page=1">首页</a></li>'.format(self.url_prefix))
        # 加一个上一页
        if self.current_page == 1:
            l.append('<li class="disabled" ><a href="#">«</a></li>'.format(self.current_page))
        else:
            l.append('<li><a href="{}?page={}">«</a></li>'.format(self.url_prefix, self.current_page - 1))
        for i in range(self.page_start, self.page_end + 1):

            if i == self.current_page:
                tmp = '<li class="active"><a href="{0}?page={1}">{1}</a></li>'.format(self.url_prefix, i)
            else:
                tmp = '<li><a href="{0}?page={1}">{1}</a></li>'.format(self.url_prefix, i)
            l.append(tmp)

        # 加一个下一页
        if self.current_page == self.page_num:
            l.append('<li class="disabled"><a href="#">»</a></li>'.format(self.current_page))
        else:
            l.append('<li><a href="{}?page={}">»</a></li>'.format(self.url_prefix, self.current_page + 1))
        # 加一个尾页
        l.append('<li><a href="{}?page={}">尾页</a></li>'.format(self.url_prefix, self.page_num))
        return "".join(l)

 

views:

from django.shortcuts import render
from app01 import models
# Create your views here.
def publisher_list(request):
    # 查询出所有的数据
    data = models.Press.objects.all()

    data_num = data.count()  # 数据的总数

    current_page = request.GET.get("page", 1)


    from utils import mypage
    obj = mypage.Pagination(data_num, current_page, "/publisher_list/")
    # 对数据做切片
    publisher_list = data[obj.start:obj.end]

    page_html = obj.page_html()
    return render(
        request,
        "publisher_list.html",
        {"publisher_list": publisher_list, "page_html": page_html})

html页面:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <table class="table table-bordered">
    <thead>
    <tr>
        <th>#</th>
        <th>ID</th>
        <th>出版社名称</th>
    </tr>
    </thead>
    <tbody>
    {% for publisher in publisher_list %}
        <tr>
        <td>{{ forloop.counter }}</td>
        <td>{{ publisher.id}}</td>
        <td>{{ publisher.name }}</td>
        </tr>
    {% endfor %}

    </tbody>
</table>
<nav aria-label="...">
      <ul class="pagination">
              {{ page_html|safe }}
     </ul>
   </nav>
</div>
<script src="/static/jquery-3.2.1.min.js"></script>
<script src="/static/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>

 

 
 
 
posted @ 2018-04-12 23:34  带带大师兄丶  阅读(154)  评论(0编辑  收藏  举报
欢迎第 Free Counters个访客