python 分页
#!/usr/bin/env python # -*- coding:utf-8 -*- class PageInfo(object): def __init__(self,current_page,per_page_num,all_count,base_url,page_range=7): """ :param current_page: 当前页 :param per_page_num: 每页显示数据条数 :param all_count: 数据库总个数 :param base_url: 开始页 """ try: current_page = int(current_page) except Exception as e: current_page = int(1) self.current_page = current_page self.per_page_num = per_page_num self.all_count = all_count self.base_url = base_url self.page_range = page_range #计算总页数 a,b = divmod(all_count,per_page_num) if b != 0: self.all_page = a + 1 else: self.all_page = a def start(self): return (self.current_page - 1) * self.per_page_num def end(self): return self.current_page * self.per_page_num def page_str(self): """ 在HTML页面中显示页码信息 :return: """ page_list = [] if self.current_page <=1: prve = '<li><a href="#">上一页</a></li>' else: prve = '<li><a href="%s?p=%s">上一页</a></li>' %(self.base_url,self.current_page-1,) page_list.append(prve) ###中间具体页数字 #只有8页 if self.all_page <= self.page_range: start = 1 end = self.all_page + 1 else: #页数18 if self.current_page > int(self.page_range/2): #当前页6,7,8,9,100 if (self.current_page + int(self.page_range/2)) > self.all_page: start = self.all_page - self.page_range + 1 end = self.all_page + 1 else: start = self.current_page - int(self.page_range/2) end = self.current_page + int(self.page_range/2) + 1 else: start = 1 end = self.page_range + 1 for i in range(start,end): #选中哪个颜色深 if self.current_page == i: temp = '<li class="active"><a href="%s?p=%s">%s</a></li>' %(self.base_url,i,i) else: temp = '<li><a href="%s?p=%s">%s</a></li>' % (self.base_url, i, i) page_list.append(temp) if self.current_page >= self.all_page: nex = '<li><a href="#">下一页</a></li>' else: nex = '<li><a href="%s?p=%s">下一页</a></li>' %(self.base_url,self.current_page+1,) page_list.append(nex) return "".join(page_list)
调用
from utils.page import PageInfo def users2(request): #s所有的条数 all_count = models.UserInfo.objects.all().count() page_info = PageInfo(request.GET.get('p'),10,all_count,request.path_info) #request.path_info 当前页面 # user_list = models.UserInfo.objects.all()[0:10] #1 # user_list = models.UserInfo.objects.all()[10:20] # 2 # user_list = models.UserInfo.objects.all()[20:30] # 3 #每页需要展示的具体条数 user_list = models.UserInfo.objects.all()[page_info.start():page_info.end()] return render(request,'users2.html',{'user_list':user_list,'page_info':page_info})
前端html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="/static/bootstrap-3.3.7/css/bootstrap.css"> </head> <body> <div style="width: 700px;margin: 0 auto"> <h1>用户列表</h1> <div> <a href="/add-user" class="btn btn-default">添加</a> </div> <table class="table table-hover table-bordered"> <thead> <tr> <th>用户名</th> <th>密码</th> <th>邮箱</th> </tr> </thead> <tbody> {% for row in user_list %} <tr> <td>{{ row.username }}</td> <td>{{ row.password }}</td> <td>{{ row.email }}</td> </tr> {% endfor %} </tbody> </table> <nav aria-label="..."> <ul class="pagination"> {{ page_info.page_str|safe }} </ul> </nav> </div> </body> </html>