分页

	分页器:
		-干啥的?数据量大的话,可以分页获取,查看
		基本写法:
			后端:
				-总数据拿出来
				-生成分页器Paginator对象(对象里有属性和方法)
				-生成当前页的对象,current_page=paginator.page(当前页码)
				-取出前台传过来的页码,current_page_num = int(request.GET.get('page'))
					-需要有异常捕获
					-捕获到,把当前页码设置成第一页
			
			
			前端:
				-for循环总页列表
				-点某一页,跳到指定页码,<li><a href="/index/?page={{ foo }}">{{ foo }}</a></li>
				
				-上一页,下一页的处理:需要有判断,判断是否有上一页,下一页
		-终极版:
			#判断总页码数是否大于11,不大于11,走else:把总页码数,赋给page_range(前端循环页码列表,循环的就是page_range)
			if paginator.num_pages >11:
				# 当前页码数-5大于1的时候,page_range应该是,page_range就是1到11页?
				if current_page_num-5<1:
					page_range=range(1,12)
				elif current_page_num+5>paginator.num_pages:
					# 当前页码数+5大于总页码数,总页码-10,到总页码+1之间
					page_range=range(paginator.num_pages-10,paginator.num_pages+1)
				else:
					#其他情况,左5,右6推算,也就是:range(current_page_num - 5, current_page_num + 6)
					page_range = range(current_page_num - 5, current_page_num + 6)
			else:
				#小于11,有多少页,就显示多少页
				page_range=paginator.page_range
			
		
	批量插入输入:
		-models.Book.objects.bulk_create(ll)

代码
示范
from django.shortcuts import render, HttpResponse

# Create your views here.
from django.core.paginator import Paginator, EmptyPage
from app01 import models


# def index(request):
# # 初级方法
# # for i in range(100):
# # models.Book.objects.create(name='图书%s'%i,price=10+i)
# # 进阶方法(批量插入)
# # 先造成100本书,放到列表中
# # ll=[]
# # for i in range(100):
# # ll.append(models.Book(name='图书%s'%i,price=10+i))
# # # 批量插入,两个参数,第一个是对象列表,第二个是一次插入的数据量,不填,默认一次全插入
# # models.Book.objects.bulk_create(ll)
#
# # 查询所有图书
# book_list=models.Book.objects.all()
# # 分页器--类
# # 实例化产生一个对象
# # 两个参数:object_list:对象列表, per_page:每页显示的条数
# paginator=Paginator(book_list,10)
# # 对象内的属性
# # 数据总条数100条
# # print(paginator.count)
# # # 总页数 10页
# # print(paginator.num_pages)
# # # 页码数的列表
# # print(paginator.page_range)
# # # 取到第 x 页 ,返回一个Page对象
# # current_page=paginator.page(5)
# # # 当前页码内所有的数据
# # print(current_page.object_list)
# # # 是否有下一页
# # print(current_page.has_next())
# # # 是否有上一页
# # print(current_page.has_previous())
# # # 下一页页码数
# # print(current_page.next_page_number())
# # # 上一页的页码数
# # print(current_page.previous_page_number())
#
#
# return render(request,'index.html',locals())

# def index(request):
# book_list = models.Book.objects.all()
#
# paginator = Paginator(book_list, 3)
# # 当前页码
# try:
# current_page_num = int(request.GET.get('page'))
# current_page = paginator.page(current_page_num)
# # 当前页码所有数据
# print(current_page.object_list)
# # 即可以循环current_page.object_list,又可以循环当前页的对象
# # for item in current_page:
# # print(item.name)
# # except EmptyPage as e:
# # # 捕获异常后,跳到第一页
# # current_page_num = 1
# # current_page = paginator.page(current_page_num)
# except Exception as e:
# current_page_num = 1
# current_page = paginator.page(current_page_num)
#
# return render(request, 'index.html', locals())

def index(request):
book_list = models.Book.objects.all()
paginator = Paginator(book_list, 3)
# 如果页码数多,让它显示前5,后5,中间是当前在的页码
try:

current_page_num = int(request.GET.get('page'))
current_page = paginator.page(current_page_num)
print(current_page.object_list)
# 总页码数,大于11的时候
if paginator.num_pages >11:
# 当前页码数-5大于1的时候,page_range应该是?
if current_page_num-5<1:
page_range=range(1,12)
elif current_page_num+5>paginator.num_pages:
# 当前页码数+5大于总页码数,总页码数往前推11个
page_range=range(paginator.num_pages-10,paginator.num_pages+1)
else:
page_range = range(current_page_num - 5, current_page_num + 6)
else:
#小于11,有多少页,就显示多少页
page_range=paginator.page_range
except Exception as e:
current_page_num = 1
current_page = paginator.page(current_page_num)

return render(request, 'index_next.html', locals())
 
posted @ 2018-11-20 18:48  不沉之月  阅读(93)  评论(0编辑  收藏  举报