分批次获取数据是分页的真相。 models.UserInfo.objects.all()[0:10] models.UserInfo.objects.all()[10:20] result = models.UserInfo.objects.all() return render(request,'index.html',{'user_list':result}) django自带的分页: from django.core.paginator import Paginator,Page paginator = Paginator(result,10) 这样,paginator可以加的参数: per_page:每页显示条目数量 count:数据总个数 num_pages:总页数 page_range:总页数的索引范围 page:page对象,当前页数 current_page = request.GET.get('page') post = paginator.page(current_page) post对象的属性: has_next:是否有写一页 next_page_number :下一页页码 has_previous:是否有上一页 previous_page_number:上一页页码 object_list:分页之后的数据列表 number:当前页 paginator:paginator对象 return render(request,'index.html',{'post':post}) {% for row in post.object_list %} <li>{{ row.name }}</li> {% endfor %} <div> {% if post.has_previous %} <a href='/index.html?page={{ post.previous_page_number }}>上一页</a> {% endif %} {% if post.has_next %} <a href='/index.html?page = {{ post.next_page_number }}>下一页</a> {% endif %} </div> 如果页面参数得到非整数型的数值,则会触发异常。下面在触发异常时转到第一页: try: post = paginator.page(current_page) except PageNotAnInteger as e: post = paginator.page(1) 注意需要增加:from django.core.paginator import PageNotAnInteger 在上一页和下一页中间增加页面数: {% for num in post.paginator.page_range %} <a href='index.html?page={{ num }}>{{ num }} </a> {% endfor %} Django自带的分页功能,仅能做到这些,必须将全部页面全部列出,无法隐藏一些页面数。 仅有上一页或者下一页 自行开发分页功能: current_page = request.GET.get('page') #用户当前要访问的页码 current_page = int(current_page) per_page = 10 #每页显示的数据条数 user_list = models.UserInfo.objects.all()[(current_page-1)*per_page:current_page*per_page] return render(request,'custom.html',{'user_list':user_list}) custom.html: {% for row in user_list %} <li>{{ row.name }}</li> {% endfor %} 另外可以采用类的方式实现: class PageInfo(object): def __init__(self,current_page,per_page): try: self.current_page = int(current_page) except Exception as e: self.current_page = 1 #self.current_page = current_page self.per_page = per_page def start(self): return (self.current_page - 1)* self.per_page def end(self): return self.current_page*self.per_page def custom(page): current_page = request.GET.get('page') #current_page = int(current_page) page_info = PageInfo(current_page,10) user_list = models.UserInfo.objects.all()[page_info.start():page_info.end()] 这样,实现了代码的简化。 让页面显示所有的页码: 首先计算总共的页数: all_count = models.UserInfo.objects.all().count() class PageInfo(obejct): def __init__(self,current_page.all_count,per_page): try: self.current_page = int(current_page) except Exception as e: self.current_page = 1 self.per_page = per_page a,b = divmod(all_count,per_page) if b: a = a + 1 self.all_page = a def pager(self): #v = "<a href='/custom.html?page=1'>1</a>" #return v page_list = [] for i in range(1,self.all_page+1): if i == self.current_page: temp = "<a style='dispaly:inline-block;padding:5px;margin:5px;background-color:red;' href='/custom.html?page=%s>%s</a>" % (i,i,) else: temp = "<a style='dispaly:inline-block;padding:5px;margin:5px' href='/custom.html?page=%s>%s</a>" % (i,i,) page_list.append(temp) return ''.join(page_list) 在custom.html中增加传递函数名,则自动执行该函数: {{ page_info.pager|safe }} 让页面仅显示当前页面及相近的几个页码: 例如显示当前页前后5个及当前页共计11页: class PageInfo(object): def __init__(self,current_page,all_count,per_page,show_page=11): ···· self.show_page = show_page 改写pager函数: def pager(self): page_list = [] half = int(self.show_page=1) /2 begin = self.current_page - half stop = self.current_page + half +1 for i in range(begin,stop): ······ 这样在如下情况会出问题:1、页码会出现小于0的;2、如果页码总数比11页少,也会显示11页。 if self.all_page < self.show_page: begin = 1 stop = self.all_page + 1 else: page_list = [] #如果当前页<=5,那么直接显示1-11页页码,不必再判断: if self.current_page <= half: begin = 1 stop = self.show_page + 1 else: if (self.current_page + half) > self.all_page: stop = self.all_page + 1 begin = self.all_page - self.show_page + 1 else: begin = self.current_page - half stop = self.current_page + half + 1 if self.current_page <= 1: prev = "<a href='#'>上一页</a>" else: prev = "<a href='/custom.html?page=%s'>上一页</a>" % (self.current_page-1) 下面将老师、班级、学生管理页面都加上分页: page_info = PageInfo(request.GET.get('page'),all_count,10,'/custom.html') 也就是将html a标签的地址也作为参数,这样,学生和老师可以写不同的网址了。 def __init__参数中加上base_url 可以使用bootstrap的分页组件,使得页面更漂亮一些: <li><a href='$'>1</a></li> 修改pager函数。 当前页在li标签中增加class='active'属性。 最后把class放在工具文件夹utils中,然后把类加入文件pager.py中: from utils.pager import PageInfo 就可以使用了。