首页 > 其他 > 详细

Django 简易版分页器

时间:2020-10-30 19:05:38      阅读:38      评论:0      收藏:0      [点我收藏+]
from django.shortcuts import render,HttpResponse
from app01 import models

#
# def user_list(request):
# for i in range(500):
# dic = {‘name‘:‘name_%d‘ % i, ‘age‘:i}
# models.User.objects.create(**dic)
# return HttpResponse(‘OK‘)
#
#


def user_list(request):
current_page = request.GET.get(‘page‘,1)
print(current_page)
current_page = int(current_page)
start1 = (current_page-1)*10
end1 = current_page*10
all_item = models.User.objects.all().count()
all_page ,div = divmod(all_item, 10)
if div > 0:
all_page +=1
page_str = ‘‘
if all_page <=11:
start = 11
end = all_page
else:
if current_page <=6:
start = 1
end = 11 + 1
else:
start = current_page - 5
end = current_page + 6
if current_page + 6 > all_page:
start = all_page - 10
end = all_page + 1
for i in range(start,end):
if i == current_page:

temp = ‘<a style="color:red; font-size:26px;padding: 5px" href="/user_list/?page=%d">%d</a>‘% (i,i)
else:
temp = ‘<a href="/user_list/?page=%d">%d</a>‘ % (i, i)

page_str += temp


user_list = models.User.objects.all()[start1:end1]
return render(request,‘user_list.html‘,{‘user_list‘:user_list, ‘page_str‘:page_str})

Django 简易版分页器

原文:https://www.cnblogs.com/niucunguo/p/13903394.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!