django的分页系统
django自带的分页
from django.contrib import admin from django.urls import path,re_path from app01 import views urlpatterns = [ re_path(r'^index.html', views.index), ]
def index(request): current_page = request.GET.get('page') user_list=models.UserInfo.objects.all() paginator = Paginator(user_list,10) # per_page: 每页显示条目数量 # count: 数据总个数 # num_pages:总页数 # page_range:总页数的索引范围,如: (1,10),(1,200) # page: page对象 try: posts=paginator.page(current_page) except Exception as e: posts = paginator.page(1) # has_next 是否有下一页 # next_page_number 下一页页码 # has_previous 是否有上一页 # previous_page_number 上一页页码 # object_list 分页之后的数据列表 # number 当前页 # paginator paginator对象 return render(request,'index.html',{'posts':posts})
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <ul> {% for i in posts.object_list %} <li>{{ i.name }}</li> {% endfor %} </ul> <div> {% if posts.has_previous %} <a href="/index.html?page={{ posts.previous_page_number }}">上一页</a> {% endif %} {% for num in posts.paginator.page_range %} <a href="/index.html?page={{ num }}">{{num}}</a> {% endfor %} {% if posts.has_next %} <a href="/index.html?page={{ posts.next_page_number }}">下一页</a> {% endif %} </div> </body> </html>
自己写的分页类
class PageInfo(): def __init__(self,base_url,current_page,per_page,all_count,show_page): ''' :param base_url: 需要的url :param current_page: 当前页码,一般用get请求获得 :param per_page: 每一页显示多少条数据 :param all_count: 数据总条数 :param show_page: 显示的总条数,例如显示34567,随着页码变换,也会动态变动 ''' try: self.current_page=int(current_page) except Exception as e: self.current_page=1 self.base_url=base_url self.per_page=per_page self.all_count=all_count self.show_page=show_page a,b=divmod(self.all_count,self.per_page) if b: a=a+1 self.all_pager=a def start(self): return (self.current_page - 1) * self.per_page def end(self): return self.current_page * self.per_page def pager(self): # v="<a href='/custom.html?page=1'>1</a>" # return v page_list=[] half=int((self.show_page-1)/2) if self.all_pager<self.show_page: begin=1 stop=self.all_pager+1 else: if self.current_page<=half: begin=1 stop=self.show_page+1 elif self.current_page>self.all_pager-half: begin=self.all_pager-self.show_page+1 stop=self.all_pager+1 else: begin=self.current_page-half stop=self.current_page+half+1 if self.current_page<=1: prev="<a style='display:inline-block; margin:5px' >上一页</a>" else: prev="<a style='display:inline-block; margin:5px' href='%s?page=%s'>上一页</a>"%(self.base_url,self.current_page-1) page_list.append(prev) for i in range(begin,stop): if i==self.current_page: temp="<a style='display:inline-block; background-color:red; margin:5px' href='%s?page=%s'>%s</a>"%(self.base_url,i,i) else: temp="<a style='display:inline-block; margin:5px' href='%s?page=%s'>%s</a>"%(self.base_url,i,i) page_list.append(temp) if self.current_page>=self.all_pager: nex="<a style='display:inline-block; margin:5px' >下一页</a>" else: nex="<a style='display:inline-block; margin:5px' href='%s?page=%s'>下一页</a>"%(self.base_url,self.current_page+1) page_list.append(nex) return ''.join(page_list)
写出漂亮的博客就是为了以后看着更方便的。