Django 简易版分页器
Posted niucunguo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django 简易版分页器相关的知识,希望对你有一定的参考价值。
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 简易版分页器的主要内容,如果未能解决你的问题,请参考以下文章