手撸分页器
批量插入数据
在撸分页器之前,先往数据库中插入大量的数据做备用
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杂录
下一篇:子查询、事务
spc文件怎么看,spc文件用什么打开?
0文件怎么看,0文件用什么打开?
sparseimage文件怎么看,sparseimage文件用什么打开?
sp文件怎么看,sp文件用什么打开?
dv文件怎么看,dv文件用什么打开?
soundpack文件怎么看,soundpack文件用什么打开?
dus文件怎么看,dus文件用什么打开?
dtw文件怎么看,dtw文件用什么打开?
spdf文件怎么看,spdf文件用什么打开?
0文件怎么看,0文件用什么打开?
