博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义分页
阅读量:5140 次
发布时间:2019-06-13

本文共 8133 字,大约阅读时间需要 27 分钟。

一:封装保存

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 += '
' 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 += '
' return tab_html,start_num,end_num

三:批量添加数据

一般测试分页都需要大量的数据 ,下面一次性的批量添加一些数据在项目里创建一个py文件,名字随便起,但是需要注意: os.environ.setdefault("DJANGO_SETTINGS_MODULE", "crm.settings") 该配置必须是当前项目 manage.py的配置,如果不是则会报错建议直接复制粘贴过来这个配置,避免额外的错误右键执行,这样就批量创建好了数据,最后查看表里有没有创建成功数据
import osif __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)

转载于:https://www.cnblogs.com/Quantum-World/p/11024213.html

你可能感兴趣的文章
经典问题-生产者和消费者问题
查看>>
Hadoop Distributed File System 简介
查看>>
文档通信(跨域-不跨域)、时时通信(websocket)、离线存储(applicationCache)、开启多线程(web worker)...
查看>>
常用正则表达式
查看>>
队列的基本使用方法
查看>>
解题:USACO18FEB Taming the Herd
查看>>
ACM-括号匹配问题
查看>>
使用Python中的urlparse、urllib抓取和解析网页(一)(转)
查看>>
Linux_屏蔽360、scanv、QQ管家等IP扫描
查看>>
LeetCode 538. Convert BST to Greater Tree
查看>>
@JoinColumn
查看>>
22_传智播客iOS视频教程_类的定义
查看>>
HDU 1856
查看>>
[HDU 2102] A计划(搜索题,典型dfs or bfs)
查看>>
推荐给4.3.3越狱用户的安全漏洞修复工
查看>>
用HTML的select+option标签实现下拉框
查看>>
[改善Java代码]asList方法产生的List对象不可更改
查看>>
ACM题目————Find them, Catch them
查看>>
LeetCode Weekly Contest 119
查看>>
Angular2-之开发环境搭建/调试环境配置
查看>>