手撸分页器


批量插入数据

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

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_querysetount() # 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_page5,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',locals())

上一篇:Django杂录

下一篇:子查询、事务


Copyright © 2002-2019 k262电脑网 www.k262.cn 皖ICP备2020016292号
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!QQ:251442993 热门搜索 网站地图