django之分页,纯python代码

Django中分页

py文件代码

"""
自定义分页组件
可以返回分页的数据和分页的HTML代码
"""
from django.http import QueryDict


class Pagination(object):

    def __init__(self, current_page, total_count, url_prefix, query_dict=QueryDict(mutable=True), per_page=10,
                 show_page=9):
        """
        初始化分分页器
        :param url_prefix: a标签的URL前缀
        :param current_page: 当前页码数
        :param total_count: 数据总数
        :param query_dict: 空的QueryDict()对象,并且是可修改的
        :param per_page: 每一页显示多少数据, 默认值是10
        :param show_page: 页面显示的页码数, 默认值是9
        """
        # 0.分页的URL前缀
        self.url_prefix = url_prefix
        self.query_dict = query_dict
        # 1. 每一页显示10条数据
        self.per_page = per_page
        assert per_page > 0
        # 2. 计算需要多少页
        total_page, more = divmod(total_count, per_page)
        if more:
            total_page += 1
        self.total_page = total_page
        # 3. 当前页码
        try:
            current_page = int(current_page)
        except Exception as e:
            current_page = 1
        current_page = total_page if current_page > total_page else current_page
        # 页码必须是大于0的数
        if current_page < 1:
            current_page = 1

        self.current_page = current_page
        # 4. 页面最多显示的页码数
        self.show_page = show_page
        # 5. 最多显示页码的一半
        self.half_show_page = self.show_page // 2

    @property
    def start(self):
        # 数据切片的开始位置
        return self.per_page * (self.current_page - 1)

    @property
    def end(self):
        # 数据切片的结束为止
        return self.current_page * self.per_page

    # 定义一个返回HTML代码的方法
    def page_html(self):
        # 如果总页码数小于最大要显示的页码数
        if self.total_page < self.show_page:
            show_page_start = 1
            show_page_end = self.total_page
        # 左边越界
        elif self.current_page - self.half_show_page < 1:
            show_page_start = 1
            show_page_end = self.show_page
        # 右边越界
        elif self.current_page + self.half_show_page > self.total_page:
            show_page_end = self.total_page
            show_page_start = self.total_page - self.show_page + 1
        else:
            show_page_start = self.current_page - self.half_show_page
            # 页面显示页码的结束
            show_page_end = self.current_page + self.half_show_page
        # 生成分页的HTML代码
        page_list = []
        # 添加分页代码的前缀
        page_list.append('<nav aria-label="Page navigation"><ul class="pagination">')
        # 添加首页
        self.query_dict['page'] = 1
        page_list.append(f'<li><a href="{self.url_prefix}?{self.query_dict.urlencode()}">首页</a></li>')
        # 添加上一页
        if self.current_page - 1 < 1:  # 已经到头啦,不让点上一页啦
            page_list.append(
                '<li class="disabled"><a href="" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>')
        else:
            self.query_dict['page'] = self.current_page - 1
            page_list.append(
                f'<li><a href="{self.url_prefix}?{self.query_dict.urlencode()}" aria-label="Previous">'
                f'<span aria-hidden="true">&laquo;</span></a></li>'
            )
        for i in range(show_page_start, show_page_end + 1):
            self.query_dict['page'] = i
            if i == self.current_page:
                s = f'<li class="active"><a href="{self.url_prefix}?{self.query_dict.urlencode()}">{i}</a></li>'
            else:
                s = f'<li><a href="{self.url_prefix}?{self.query_dict.urlencode()}">{i}</a></li>'
            page_list.append(s)
        # 添加下一页
        if self.current_page + 1 > self.total_page:
            page_list.append(
                '<li class="disabled"><a href="" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>')
        else:
            self.query_dict['page'] = self.current_page + 1
            page_list.append(
                f'<li><a href="{self.url_prefix}?{self.query_dict.urlencode()}" aria-label="Next">'
                f'<span aria-hidden="true">&raquo;</span></a></li>'
            )
        # 添加尾页
        self.query_dict['page'] = self.total_page
        page_list.append(f'<li><a href="{self.url_prefix}?{self.query_dict.urlencode()}">尾页</a></li>')
        # 添加分页代码的后缀
        page_list.append('</ul></nav>')
        page_html = ''.join(page_list)
        return page_html


实例化对象

  1. def dashboard(request):
        # 获取当前网页路径
        url_prefix = request.path_info
        # 获取当前页码数
        current_page = request.GET.get('page', 1)
        # 获取所有客户的信息
        customer_list = Customer.objects.all()
        # 实例分页代码
        pagination = Pagination(current_page=current_page, total_count=customer_list.count(), url_prefix=url_prefix, per_page=8)
        # 获取当前页的数据
        data = customer_list[pagination.start:pagination.end]
        page_html = pagination.page_html()
        return render(request, "Dashboard.html", {'customer_list': data, "page_html": page_html})
    

前端代码

  1. <div>
        {{ page_html|safe }}
    </div>
    

使用django脚本去创建数据

  1. 批量创建数据
    1. 使用Django脚本去创建数据
    2. bulk_create()
    3. 5VBBw9.md.png
  2. 分页
    1. 纯Python基础写的,
    2. django里面获取当前url
      1. request.path_info 获取当前路径
      2. request.get_full_path 获取全路径

实际样式

  1. 5VDjra.png
posted @ 2019-03-04 16:24  云丛  阅读(300)  评论(0编辑  收藏  举报