django自带的分页虽然挺好,但是就想自己弄个通用的
自己写了个分页的类,用的是python, 其他语言改下语法就能用了.
#定义好类.
class pagemanage:
def __init__(self,page_each,data_count,cur_page,pageint,url):
''' :param page_each: 每次显示多少页
:param data_count: 数据总条数
:param cur_page: 当前页面数
:param pageint: 每一页显示的数据条数
:param url:渲染地址
'''
self.page_each=page_each
self.cur_page = cur_page
self.pageint = pageint
self.get_url=url
self.data_count=data_count @property#获取总共的页数
def get_countpage(self):
a, v = divmod(self.data_count, self.pageint)
self.page_count = a # page_count总的页数 if v != 0:
self.page_count = a + 1
if self.cur_page >self.page_count:
self.cur_page=self.page_count
return self.page_count # 获取每次页面显示的区间
@property
def get_interval_page(self):
page_count = self.get_countpage
if self.page_each>page_count:
start=1
end=page_count
return [start, end]
elif self.cur_page<self.page_each/2:
start = 1
end=self.page_each
return [start, end]
elif self.cur_page>page_count-self.page_each/2:
start=page_count-self.page_each+1
end=page_count
return [start, end]
else:
if self.page_each%2==0:
start=self.cur_page-self.page_each/2+1
end=self.cur_page+self.page_each/2 else:
start = self.cur_page - self.page_each / 2 #python3请改成 '//'
end = self.cur_page + self.page_each / 2 #python3请改成 '//'
return [start,end] def get_pageint(self): start = (self.cur_page - 1) * self.pageint end = (self.cur_page) * self.pageint page_start_end=(start,end) return page_start_end #将分页渲染成html标签 字符串 def getpager(self): page_list = [] if self.cur_page == 1: page_list.append("<a href=''>上一页</a>") else: page_list.append("<a href='/%s/?p=%s'>上一页</a>" % (self.get_url,self.cur_page - 1)) for i in range(self.get_interval_page[0], self.get_interval_page[1]+1): if i == self.cur_page: page_list.append("<a style='background-color: black ' href='/%s/?p=%s'>%s</a>" % (self.get_url,i, i)) continue page_list.append("<a href='/%s/?p=%s'>%s</a>" % (self.get_url,i, i)) if self.cur_page == self.get_countpage: page_list.append("<a href=''>下一页</a>") else: page_list.append("<a href='/%s/?p=%s'>下一页</a>" % (self.get_url,self.cur_page + 1)) pager = "".join(page_list) return pager
使用---------views
def xxxx(req):
cur_page = int(req.GET.get("p", 1)) # 默认为1,获取当前页码
data_count = models.Student.objects.all().count()#数据的总数
p = pagemanage(2,data_count,cur_page, 10, "mmm/getstudentdata")#使用自己写好的分页类
pager = p.getpager()#获取最后返回的字符串,就是包含标签的字符串
pageint = p.get_pageint()#获取到本次的显示页的起始数据和结束数据 data = models.Student.objects.all()[pageint[0]:pageint[1]]#获取显示的数据 return render(req, 'studentm.html', {'data': data, 'page_set': pager})
html
<table> {% for i in data %}//循环显示每条内容
<tr>
<td>{{ i.id }}</td>
<td>{{ i.name }}</td>
<td>{{ i.cls.name }}</td>
<td><a class="modify" href="/mmm/modify?nid={{ i.id }}">编辑</a>|<a href="javascript:void(0)">删除</a></td>
</tr>
{% endfor %}
</table> <div class="pagemode">//显示分页
{{ page_set|safe }}
</div>