一、分页
1.初级分页
views创建
# 创造数据 USER_LIST=[] for i in range(1,999): temp={'name':"root"+str(i),"age":i} USER_LIST.append(temp) def index(request): per_page_count=10 # 每页显示条数 current_page=request.GET.get('p') # 获得p值 ?p=值 当前页码 current_page=int(current_page) # 变成字符串 start=(current_page-1) * per_page_count #起始位置 当前减1乘以显示条数 (根据条数分页) end=(current_page)*per_page_count #结束位置 页码 * 总条数 data=USER_LIST[start:end] 数据 范围 prve1=current_page-1 当前页-1 上一页 next1=current_page+1 当前页+1 下一页 return render(request,'index.html',{'user_list':data,"prve1":prve1,'next1':next1})
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body>
<ul> {% for i in user_list %} <li><p>{{i.name}}-------{{ i.age }}</p></li> {% endfor %} </ul>
<a href="index?p={{ prve1}}">上一页</a> <a href="index?p={{ next1 }}">下一页</a> </body> </html>
2.进阶分页
views
# 创造数据 USER_LIST=[] for i in range(1,999): temp={'name':"root"+str(i),"age":i} USER_LIST.append(temp) def index1(request): # Paginator的属性 # per_page 每页显示条目数量 # count:数据总页数 # num_pages:总页数 # page_range:总页面的索引范围,例如(1,10)(1,200) # page: page对象(是否具有下一页;是否具有上一页) #全部数据: current_page=request.GET.get('p') # 当前页码 paginator=Paginator(USER_LIST,11) # Paginatord对象 try: # Page对象 posts=paginator.page(current_page) posts.has_next() # 判断是否具有下一页 posts.next_page_number() # 下一页页码 posts.has_previous() # 是否具有上一页 posts.previous_page_number() # 上一页页码 # object_list # 分页之后的数据列表,已经切片好的数据 # mumber 当前页 # paginator paginator对象 except PageNotAnInteger: posts=paginator.page(1) except EmptyPage: posts=paginator.page(paginator.num_pages) return render(request,'index1.html',{'posts':posts})
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <ul> {% for row in posts.object_list %} <li> {{ row.name }}--{{ row.age }} </li> {% endfor %} </ul> {#{% include 'include/pager.html'%}#} {% if posts.has_previous %} <a href="index1?p={{ posts.previous_page_number }}">上一页</a> {% else %} <a href="#">上一页</a> {% endif %} {% for i in posts.paginator.pager_num_range %} {% if i == posts.number %} <a style="color: #d43f3a" href="/index1?p={{i}}">{{ i }}</a> {% else%} <a href="/index1?p={{i}}">{{ i }}</a> {% endif %} {% endfor %} {% if posts.has_next %} <a href="/index1?p={{ posts.next_page_number }}">下一页</a> {% endif %} <span>{{ posts.number }}/{{ posts.paginator.num_pages }}</span> </body> </html>
3升级版 分页
# 创造数据 USER_LIST=[] for i in range(1,999): temp={'name':"root"+str(i),"age":i} USER_LIST.append(temp) 自定制Paginator类 class CustonPaginator(Paginator): def __init__(self,current_page,per_pager_num,*args,**kwargs): self.current_page=int(current_page) #当前页 self.per_pager_num=int(per_pager_num) # 最多显示页码 super(CustonPaginator,self).__init__(*args,**kwargs) def pager_num_range(self): # 当前页 # self.current_page # 最多显示页码 # self.per_pager_num = int(per_pager_num) # 总页数 # self.num_pages if self.num_pages<self.per_pager_num: return range(1,self.per_pager_num+1) part=int(self.per_pager_num/2) # 总页比较多 if self.current_page<=part: return range(1,self.per_pager_num+1) if (self.current_page+part)>self.num_pages: # 页面最后显示没有 return range(self.num_pages-self.per_pager_num,self.num_pages+1) return range(self.current_page-part,self.current_page+part+1)
def index1(request):
#全部数据:
current_page=request.GET.get('p')
# 当前页码
paginator=CustonPaginator(current_page,7,USER_LIST,11)
try:
posts=paginator.page(current_page)
except PageNotAnInteger:
posts=paginator.page(1)
except EmptyPage:
posts=paginator.page(paginator.num_pages)
return render(request,'index1.html',{'posts':posts})
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <ul> {% for row in posts.object_list %} <li> {{ row.name }}--{{ row.age }} </li> {% endfor %} </ul> {% if posts.has_previous %} <a href="index1?p={{ posts.previous_page_number }}">上一页</a> {% else %} <a href="#">上一页</a> {% endif %} {% for i in posts.paginator.pager_num_range %} {% if i == posts.number %} <a style="color: #d43f3a" href="/index1?p={{i}}">{{ i }}</a> {% else%} <a href="/index1?p={{i}}">{{ i }}</a> {% endif %} {% endfor %} {% if posts.has_next %} <a href="/index1?p={{ posts.next_page_number }}">下一页</a> {% endif %} <span>{{ posts.number }}/{{ posts.paginator.num_pages }}</span> </body> </html>
4.终极版
views
USER_LIST=[] for i in range(1,999): temp={'name':"root"+str(i),"age":i} USER_LIST.append(temp) def index2(request): from app01.pager import Pagination current_page=request.GET.get('p')
if not current_page:
current_page='1'
page_obj=Pagination(len(obj_list),current_page) data_list=USER_LIST[page_obj.start():page_obj.end()] return render(request,'index2.html',{'data':data_list,'page_obj':page_obj})
paper
class Pagination(object): def __init__(self,totalCount,currentPage,perPageltemNum=10,maxPageNum=7): self.total_count=totalCount # 数据总个数 try: v=int(currentPage) # 当前页 if v<=0: v=1 self.current_page=v except Exception as e: self.current_page=1 self.per_page_item_num=perPageltemNum #每页显示的条数 self.max_page_num=maxPageNum #最多显示的页码 def start(self): return ((self.current_page-1)*self.per_page_item_num) # 当前行 def end(self): return self.current_page*self.per_page_item_num @property def num_pages(self): ''' 总页面数 :return: ''' a,b=divmod(self.total_count,self.per_page_item_num) if b==0: return a return a+1 def pager_num_range(self): # self.num_pages() # self.num_pages # 当前页 # self.current_page # 最多显示的页码数量 11 # self.per_pager_num # 总页数 # self.num_pages # 显示页码判断 # 总页码小于设定最大页码数 if self.num_pages<self.max_page_num: return range(1,self.num_pages+1) part=int(self.max_page_num/2) # 当前页码小于页码总数的一半 if self.current_page<=part: return range(1,self.max_page_num) # 当前页码加上part大于页面总数的话 if (self.current_page+part)>self.num_pages: return range(self.current_page,self.current_page+part+1) return range(self.current_page) def page_str(self): page_list=[] first="<li><a href='/index2?p=1'>首页</a></li>" page_list.append(first) if self.current_page==1: prev="<li><a href='#'>上一页</a></li>" else: prev="<li><a href='/index2?p=%s'>上一页</a></li>"%(self.current_page-1) page_list.append(prev) for i in self.pager_num_range(): if i ==self.current_page: temp="<li class='active'><a href='/index2?p=%s'>%s</a></li>"%(i,i) else: temp="<li><a href='/index2?p=%s'>%s</a></li>" % (i, i) page_list.append(temp) if self.current_page==self.num_pages: nex="<li><a href='#'>下一页</a></li>" else: nex="<li><a href='/index2?p=%s'>下一页</a></li>" % (self.current_page + 1,) page_list.append(nex) last="<li><a href='/index2?p=%s'>尾页</a></li>" %(self.num_pages,) page_list.append(last) return ''.join(page_list)
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="/static/plugins/bootstrap/css/bootstrap.css"/> </head> <body> <ul> {% for row in data %} <li>{{ row.name }}-{{ row.age }}</li> {% endfor %} </ul> {% for i in page_obj.pager_num_range %} <a href="/index2?p={{ i }}">{{ i }}</a> {% endfor %} <hr/> <ul class="pagination pagination-sm"> {{ page_obj.page_str|safe }} </ul> <div style="height: 300px;"></div> </body> </html>