Django 简易版分页器

技术文章 1年前 (2020) 完美者
1,075 0

标签:分页   render   user   response   range   print   简易   div   else   

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 简易版分页器

标签:分页   render   user   response   range   print   简易   div   else   

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

版权声明:完美者 发表于 2020-10-31 2:09:30。
转载请注明:Django 简易版分页器 | 完美导航

暂无评论

暂无评论...