• 自定义分页


    一:封装保存

    1.1:page.py

    # 自定义分页
    # 带首页和尾页
    # 官方推荐,页码数为奇数
    class PageNation:
        def __init__(self,base_url,current_page_num,total_counts,request,per_page_counts=10,page_number=5,):
            '''
            :param base_url:   分页展示信息的基础路径
            :param current_page_num:  当前页页码
            :param total_counts:  总的数据量
            :param per_page_counts:  每页展示的数据量
            :param page_number:  显示页码数
            # 关键字参数写在最后面,如果有传进来的参数就用 传进来的,如果没有则用默认的
            '''
    
            self.base_url = base_url
            self.current_page_num = current_page_num
            self.total_counts = total_counts
            self.per_page_counts = per_page_counts
            self.page_number = page_number
            self.request = request
            try:
                self.current_page_num = int(self.current_page_num)
    
            except Exception:
                self.current_page_num = 1
            if self.current_page_num < 1:
                self.current_page_num = 1
                # 做一个异常处理, 如果是非字母或者负数 跳转到第一页
    
            half_page_range = self.page_number // 2
            # 计算总页数
            self.page_number_count, a = divmod(self.total_counts, self.per_page_counts)
    
    
            if a:
                self.page_number_count += 1
    
    
            if self.current_page_num > self.page_number_count:
                self.current_page_num = self.page_number_count
    
    
            if self.page_number_count <= self.page_number:
                self.page_start = 1
                self.page_end = self.page_number_count
            else:
                if self.current_page_num <= half_page_range:  #2
                    self.page_start = 1
                    self.page_end = page_number  #5
                elif self.current_page_num + half_page_range >= self.page_number_count:
                    self.page_start = self.page_number_count - self.page_number + 1
                    self.page_end = self.page_number_count
                else:
                    self.page_start = self.current_page_num - half_page_range
                    self.page_end = self.current_page_num + half_page_range
    
    
            import copy
            from django.http.request import QueryDict
    
            self.params = copy.deepcopy(request.GET)
    
            # ?condition = qq & wd = 1 & page = 3
            # params['page'] = current_page_num
            # query_str = params.urlencode()
        #数据切片依据,起始位置
        @property
        def start_num(self):
            if self.current_page_num == 0:
                self.current_page_num = 1
            start_num = (self.current_page_num - 1) * self.per_page_counts
            return start_num
    
        #数据切片依据,终止位置
        @property
        def end_num(self):
            end_num = self.current_page_num * self.per_page_counts
            return end_num
    
        # 拼接HTMl标签
        def page_html(self):
            tab_html = ''
            tab_html += '<nav aria-label="Page navigation" class="pull-right"><ul class="pagination">'
            #首页
            self.params['page'] = 1
            showye = '<li><a href="{0}?{1}" aria-label="Previous" ><span aria-hidden="true">首页</span></a></li>'.format(self.base_url,self.params.urlencode())
            tab_html += showye
            # 上一页
            if self.current_page_num == 1:
                previous_page = '<li disabled><a href="#" aria-label="Previous" ><span aria-hidden="true">&laquo;</span></a></li>'
            else:
                self.params['page'] = self.current_page_num - 1
                previous_page = '<li><a href="{0}?{1}" aria-label="Previous" ><span aria-hidden="true">&laquo;</span></a></li>'.format(
                    self.base_url,self.params.urlencode())
            tab_html += previous_page
    
            #循环生成页码标签
            for i in range(self.page_start, self.page_end + 1):
                # request.GET  {condition: qq, wd: 1,'page':1} request.GET.urlencode() condition=qq&wd=1&page=4
    
                self.params['page'] = i # {condition: qq, wd: 1,'page':1} urlencode() -- condition=qq&wd=1&page=4
    
                if self.current_page_num == i:
    
                    one_tag = '<li class="active"><a href="{0}?{2}">{1}</a></li>'.format(self.base_url, i,self.params.urlencode()) #?condition=qq&wd=1&page=3
                else:
                    one_tag = '<li><a href="{0}?{2}">{1}</a></li>'.format(self.base_url, i,self.params.urlencode())
                tab_html += one_tag
    
            # 下一页
            if self.current_page_num == self.page_number_count:
                next_page = '<li disabled><a href="#" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>'
            else:
                self.params['page'] = self.current_page_num + 1
                next_page = '<li><a href="{0}?{1}" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>'.format(self.base_url, self.params.urlencode())
            tab_html += next_page
    
            # 尾页
            self.params['page'] = self.page_number_count
            weiye = '<li><a href="{0}?{1}" aria-label="Previous" ><span aria-hidden="true">尾页</span></a></li>'.format(
                self.base_url, self.params.urlencode())
    
            tab_html += weiye
            tab_html += '</ul></nav>'
    
            return tab_html
    
    

    1.2:views.py

    # CBV 写法
    class CustomerView(View):
    
        def get(selef, request):
    
            all_customers = models.Customer.objects.all() # 获取数据总数量
    
            current_page_num = request.GET.get('page', 1)  # 当前页
            
            per_page_counts = 9  # 每页显示9条
            page_number = 9  # 总共显示9个页码
           
            total_count = all_customers.count() # 得到页数总的数据量
            
           #__init__(self,base_url,current_page_num,total_counts,request,per_page_counts=10,page_number=5,)
    # 关键字参数写在最后面,如果有传进来的参数就用 传进来的,如果没有则用默认的
    # 按照page里 参数顺序 进行传参
            page_obj = page.PageNation(request.path, current_page_num, total_count, request, per_page_counts, page_number)
            # 需要传个 request,要不然会报错
    
            all_customers = all_customers.order_by('-pk')[page_obj.start_num:page_obj.end_num]
            # 调用page里的start和end方法, page_obj.start_num:page_obj.end_num
            # -pk是数据进行倒序排列
    
            ret_html = page_obj.page_html()
            # 调用page里的拼接标签
    
            return render(request,'customers.html',{'all_customers':all_customers,'ret_html':ret_html})
        	# 返回给html页面,数据,和拼接的html
    

    二:函数版本

    #函数low版
    
    def pagenation(base_url,current_page_num,total_counts,per_page_counts=10,page_number=5):
        '''
        total_counts    数据总数
        per_page_counts   每页分多少条数据
        page_number = 页码显示多少个
        current_page_num 当前页
        :return:
        '''
        # all_objs_list = models.Customer.objects.all()
        # total_counts = all_objs_list.count()
        # per_page_counts = 10
        # page_number = 5
    
        try:
            current_page_num = int(current_page_num)
    
        except Exception:
            current_page_num = 1
    
    
        half_page_range = page_number//2
    
        #计算总页数      # 数据总数  每页分多少条数据
        page_number_count,a = divmod(total_counts,per_page_counts)
    
        if current_page_num < 1:
            current_page_num = 1
    
        if a:
            page_number_count += 1
    
        if current_page_num > page_number_count:
            current_page_num = page_number_count
    
        start_num = (current_page_num - 1) * 10
        end_num = current_page_num * 10
    
        if page_number_count <= page_number:
            page_start = 1
            page_end = page_number_count
        else:
            if current_page_num <= half_page_range:
                page_start = 1
                page_end = page_number
            elif current_page_num + half_page_range  >= page_number_count:
                page_start = page_number_count - page_number + 1
                page_end = page_number_count
            else:
                page_start = current_page_num - half_page_range
                page_end = current_page_num + half_page_range
    
        #拼接HTMl标签
        tab_html = ''
        tab_html += '<nav aria-label="Page navigation"><ul class="pagination">'
    
        #上一页
        if current_page_num == 1:
            previous_page = '<li disabled><a href="#" aria-label="Previous" ><span aria-hidden="true">&laquo;</span></a></li>'
        else:
            previous_page = '<li><a href="{0}?page={1}" aria-label="Previous" ><span aria-hidden="true">&laquo;</span></a></li>'.format(base_url,current_page_num-1)
        tab_html += previous_page
    
        for i in range(page_start,page_end+1):
    
            if current_page_num == i:
    
                one_tag = '<li class="active"><a href="{0}?page={1}">{1}</a></li>'.format(base_url,i)
            else:
                one_tag = '<li><a href="{0}?page={1}">{1}</a></li>'.format(base_url, i)
            tab_html += one_tag
    
    
        #下一页
        if current_page_num == page_number_count:
            next_page = '<li disabled><a href="#" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>'
        else:
            next_page = '<li><a href="{0}?page={1}" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>'.format(base_url,current_page_num+1)
        tab_html+=next_page
        tab_html += '</ul></nav>'
    
        return tab_html,start_num,end_num
    
    

    ## 三:批量添加数据
    一般测试分页都需要大量的数据 ,下面一次性的批量添加一些数据
    
    在项目里创建一个py文件,名字随便起,但是需要注意:
    
     os.environ.setdefault("DJANGO_SETTINGS_MODULE", "crm.settings") 
    
    该配置必须是当前项目 manage.py的配置,如果不是则会报错
    
    建议直接复制粘贴过来这个配置,避免额外的错误
    
    右键执行,这样就批量创建好了数据,最后查看表里有没有创建成功数据
    
    
    
    import os
    if __name__ == "__main__":
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "crm.settings")
        import django
        django.setup()
        from app01 import models
        import random
        l1=[]
        for i in range(1,101):
            obj=models.Customer(
                qq=''.join([str(i) for i in random.choices(range(1,10),k=11) ]),
                name='xiaohei'+str(i),
                sex=random.choice(['male','female']),
                source=random.choice(['qq','referral','website']),
                course=random.choice(['linuxL','PythonFullStack']),
            )
            l1.append(obj)
        models.Customer.objects.bulk_create(l1)
    
  • 相关阅读:
    Linux 内核优化
    Myeclipse7.5 下载 安装 注冊 注冊码 100%成功
    Nmap命令的29个实用范例
    运维工程师必会的109个Linux命令
    linux下维护服务器之常用命令
    黑客工具软件大全100套
    Sysstat性能监控工具包中20个实用命令
    100个linux常用命令
    28 个 Unix/Linux 的命令行神器
    linux命令合集
  • 原文地址:https://www.cnblogs.com/Quantum-World/p/11024213.html
Copyright © 2020-2023  润新知