码迷,mamicode.com
首页 > 其他好文 > 详细

Django 简易版分页器

时间:2020-10-31 02:09:30      阅读:16      评论:0      收藏: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!