Django内置的分页模块

自定义分页

未封装版:

  • 优点:直观
  • 缺点:代码乱,不易维护,可拓展性差
data = []

for i in range(1, 302):
    tmp = {"id": i, "name": "alex-{}".format(i)}
    data.append(tmp)

print(data)


def user_list(request):

    # user_list = data[0:10]
    # user_list = data[10:20]
    try:
        current_page = int(request.GET.get("page"))
    except Exception as e:
        current_page = 1

    per_page = 10

    # 数据总条数
    total_count = len(data)
    # 总页码
    total_page, more = divmod(total_count, per_page)
    if more:
        total_page += 1

    # 页面最多显示多少个页码
    max_show = 11
    half_show = int((max_show-1)/2)

    if current_page <= half_show:
        show_start = 1
        show_end = max_show
    else:
        if current_page + half_show >= total_page:
            show_start = total_page - max_show
            show_end = total_page
        else:
            show_start = current_page - half_show
            show_end = current_page + half_show

    # 数据库中获取数据
    data_start = (current_page - 1) * per_page
    data_end = current_page * per_page

    user_list = data[data_start:data_end]

    # 生成页面上显示的页码
    page_html_list = []
    # 加首页
    first_li = '<li><a href="/user_list/?page=1">首页</a></li>'
    page_html_list.append(first_li)
    # 加上一页
    if current_page == 1:
        prev_li = '<li><a href="#">上一页</a></li>'
    else:
        prev_li = '<li><a href="/user_list/?page={}">上一页</a></li>'.format(current_page - 1)
    page_html_list.append(prev_li)
    for i in range(show_start, show_end+1):
        if i == current_page:
            li_tag = '<li class="active"><a href="/user_list/?page={0}">{0}</a></li>'.format(i)
        else:
            li_tag = '<li><a href="/user_list/?page={0}">{0}</a></li>'.format(i)
        page_html_list.append(li_tag)

    # 加下一页
    if current_page == total_page:
        next_li = '<li><a href="#">下一页</a></li>'
    else:
        next_li = '<li><a href="/user_list/?page={}">下一页</a></li>'.format(current_page+1)
    page_html_list.append(next_li)

    # 加尾页
    page_end_li = '<li><a href="/user_list/?page={}">尾页</a></li>'.format(total_page)
    page_html_list.append(page_end_li)

    page_html = "".join(page_html_list)

    return render(request, "user_list.html", {"user_list": user_list, "page_html": page_html})

  

封装版:

  • 优点:易维护、可拓展性强
  • 缺点:逻辑相对复杂
"""
自定义分页组件
"""
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

        if self.page_num < self.page_end:
            self.page_end = self.page_num  # 如果总页面小于结束页面,那么结束页面即为总页码

    @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)

 封装版使用步骤:

  1. 将上面的一段代码复制到自定义的模块(pt文件)中
  2. 导入自定义模块
  3. 在views系统的函数里写入以下代码
    def user_list(request):
        # 实例化一个分页类的对象
        obj = Pagination(blogs.count(),page,'blogs.html')
        # 根据传入的总博客数、页码获取当页需要展示的博客
        blog_list = blogs[obj.start:obj.end]
        # 根据传入的博客数及页码获取生成分页的html格式的页码列表
        page_html = pager.page_html()
        #返回指定页面
        return render(request, "blog_list.html", {"blog_list": blog_list, "page_html": page_html})
    

      

  4. 在需要生成页面的html页面写入以下代码
    <div class="topicListFooter fenye">
         <ul class="pagination">
             {{ page_html|safe }}
          </ul>
     </div>
    

      注:示例中用的是bootstrap分页样式

Django中的内置分页(不能展示页码列表)

from django.shortcuts import render
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

L = []
for i in range(999):
    L.append(i)

def index(request):
    current_page = request.GET.get('p')

    paginator = Paginator(L, 10)
    # per_page: 每页显示条目数量
    # count:    数据总个数
    # num_pages:总页数
    # page_range:总页数的索引范围,如: (1,10),(1,200)
    # page:     page对象
    try:
        posts = paginator.page(current_page)
        # has_next              是否有下一页
        # next_page_number      下一页页码
        # has_previous          是否有上一页
        # previous_page_number  上一页页码
        # object_list           分页之后的数据列表
        # number                当前页
        # paginator             paginator对象
    except PageNotAnInteger:
        posts = paginator.page(1)
    except EmptyPage:
        posts = paginator.page(paginator.num_pages)
    return render(request, 'index.html', {'posts': posts})

内置分页view部分

  

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<ul>
    {% for item in posts %}
        <li>{{ item }}</li>
    {% endfor %}
</ul>

<div class="pagination">
      <span class="step-links">
        {% if posts.has_previous %}
            <a href="?p={{ posts.previous_page_number }}">Previous</a>
        {% endif %}
          <span class="current">
            Page {{ posts.number }} of {{ posts.paginator.num_pages }}.
          </span>
          {% if posts.has_next %}
              <a href="?p={{ posts.next_page_number }}">Next</a>
          {% endif %}
      </span>

</div>
</body>
</html>

内置分页HTML部分

  

 
posted @ 2018-04-07 00:19  人生不如戏  阅读(608)  评论(0编辑  收藏  举报