一、分页(函数)
views:
1 def host_list(request): 2 #生成主机数据 3 hosts=[] 4 for i in range(1,254): 5 temp="192.168.1."+str(i) 6 hosts.append(temp) 7 8 #根据每页数据量和当前页码对主机数据进行切片 9 per_page_count=10 10 current_page=int(request.GET.get("p",1)) 11 host_list=hosts[(current_page-1)*per_page_count:current_page*per_page_count] 12 13 #生成页码 14 total_count=len(hosts) 15 v,y=divmod(total_count,per_page_count) 16 if y: 17 v=v+1 18 total_page=v 19 tags=[] 20 21 #上一页 22 prev='<a href="/host_list/?p=%s">上一页</a>'%(current_page-1) 23 if current_page==1: 24 prev='<a href="#">上一页</a>' 25 tags.append(prev) 26 #所有页码 27 pager_num=7 #显示多少个页码 28 if current_page<pager_num/2+1: 29 start_page=1 30 end_page=pager_num+2 31 elif current_page>total_page-pager_num/2: 32 start_page = total_page-pager_num 33 end_page = total_page+1 34 else: 35 start_page=current_page-pager_num/2 36 end_page=current_page+pager_num/2+1 37 start_page=int(start_page) 38 end_page=int(end_page) 39 for i in range(start_page,end_page): 40 temp='<a href="/host_list/?p=%s">%s</a>'%(i,i) 41 tags.append(temp) 42 #下一页 43 next='<a href="/host_list/?p=%s">下一页</a>'%(current_page+1) 44 if current_page==total_page: 45 next='<a href="#">下一页</a>' 46 tags.append(next) 47 #页面跳转 48 jump=''' 49 <div> 50 <input type="text" id="i1"> 51 <a onclick="jumpTo(this,'/host_list/?p=')">Go</a> 52 </div> 53 <script> 54 function jumpTo(ths,base) { 55 var val=document.getElementById("i1").value; 56 location.href=base+val 57 } 58 </script> 59 ''' 60 tags.append(jump) 61 return render(request,"host_list.html",{"host_list":host_list,"tags":tags})
html:
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>主机页面</title> 6 </head> 7 <body> 8 <table border="1"> 9 <thead> 10 <tr> 11 <th>主机</th> 12 </tr> 13 </thead> 14 <tbody> 15 {% for host in host_list %} 16 <tr> 17 <td>{{ host }}</td> 18 </tr> 19 {% endfor %} 20 </tbody> 21 </table> 22 23 {% for tag in tags %} 24 <span>{{ tag|safe }}</span> 25 {% endfor %} 26 27 28 </body> 29 </html>
二、封装成类使用:
Page:
class Page: def __init__(self, current_page, data_count, per_page_count=10, pager_num=7): self.current_page = current_page self.data_count = data_count self.per_page_count = per_page_count self.pager_num = pager_num @property def start(self): return (self.current_page - 1) * self.per_page_count @property def end(self): return self.current_page * self.per_page_count @property def total_page(self): v, y = divmod(self.data_count, self.per_page_count) if y: v = v + 1 return v def tags(self, base_url): tags = [] # 上一页 prev = '<a href="%s?p=%s">上一页</a>' % (base_url, self.current_page - 1) if self.current_page == 1: prev = '<a href="#">上一页</a>' tags.append(prev) # 所有页码 pager_num = 7 # 显示多少个页码 #如果页数大于7: if self.total_page>=7: if self.current_page < pager_num / 2 + 1: start_page = 1 end_page = pager_num + 1 elif self.current_page > self.total_page - pager_num / 2: start_page = self.total_page - pager_num end_page = self.total_page + 1 else: start_page = self.current_page - pager_num / 2 end_page = self.current_page + pager_num / 2 + 1 else:#如果页数小于7 start_page=1 end_page=self.total_page+1 start_page = int(start_page) end_page = int(end_page) for i in range(start_page, end_page): temp = '<a href="%s?p=%s">%s</a>' % (base_url, i, i) if i==self.current_page: temp = '<a href="%s?p=%s" class="high_light">%s</a>' % (base_url, i, i) tags.append(temp) # 下一页 next = '<a href="%s?p=%s">下一页</a>' % (base_url, self.current_page + 1) if self.current_page == self.total_page: next = '<a href="#">下一页</a>' tags.append(next) # 页面跳转 jump = ''' <div> <input type="text" id="i1"> <a onclick="jumpTo(this,'%s?p=')">跳转</a> </div> <script> function jumpTo(ths,base) { var val=document.getElementById("i1").value; console.log(val) location.href=base+val } </script> ''' % base_url tags.append(jump) return tags
使用:
1 def host_list(request): 2 #伪造主机数据 3 hosts=[] 4 for i in range(1,254): 5 temp="192.168.1."+str(i) 6 hosts.append(temp) 7 8 #根据每页数据量和当前页码对主机数据进行切片 9 per_page_count=10 #每页显示个数 10 pager_num = 7 # 显示多少个页码 11 current_page=int(request.GET.get("p",1)) 12 obj=Page(current_page=current_page,data_count=len(hosts),per_page_count=per_page_count,pager_num=pager_num) 13 host_list = hosts[obj.start:obj.end] 14 15 #生成页码 16 tags=obj.tags("/host_list/") 17 18 return render(request,"host_list.html",{"host_list":host_list,"tags":tags})