zoukankan      html  css  js  c++  java
  • 手撸分页器

    批量插入数据

    在撸分页器之前,先往数据库中插入大量的数据做备用

    list = []
    for i in range(100000):
        list.append(models.Book(title = '第%s本书'%i))
    models.Book.objects.bulk_create(book_list) # 批量插入数据
    

    手撸分页器

    def index(request):
         # 1.获取用户想要访问的页码数
        current_page = request.GET.get('page',1)  # 如果没有page参数 默认就展示第一页
        # 转成整型
        current_page = int(current_page)
        # 2.每页展示10条数据
        per_page_num = 10
        # 3.定义起始位置和终止位置
        start_page = (current_page - 1) * per_page_num
        end_page = current_page * per_page_num
        # 4.统计数据的总条数
        book_queryset = models.Book.objects.all()
        all_count = book_queryset.count()
         # 5.求数据到底需要多少页才能展示完
        page_num, more = divmod(all_count,per_page_num)  # divmod(100,10)
        if more:
        	page_num += 1
        # page_num就觉得了 需要多少个页码
        page_html = ''
        xxx = current_page  # xxx就是用户点击的数字
        if current_page < 6:
        	current_page = 6
        for i in range(current_page-5,current_page+6):
        	if xxx == i:
                page_html += '<li class="active"><a href="?page=%s">%s</a></li>'%(i,i)
            else:
                page_html += '<li><a href="?page=%s">%s</a></li>' % (i, i)
        book_queryset = book_queryset[start_page:end_page]
        return render(request,'index.html',locals())
    
  • 相关阅读:
    HashTable, HashMap,TreeMap区别
    redis的多线程
    五种I/O模型介绍
    SpringBoot的优点
    spring注解
    三种方法求解两个数组的交集
    数据库七种传播行为
    BETA 版冲刺前准备
    Python学习笔记(二)--变量和数据类型
    事后诸葛亮
  • 原文地址:https://www.cnblogs.com/jie9527-/p/11764489.html
Copyright © 2011-2022 走看看