首页 > 代码库 > html_helper 分页

html_helper 分页

 1 # -*- coding: utf-8 -*-
 2 
 3 from django.utils.safestring import mark_safe
 4 ‘‘‘
 5 Created on 2017年5月4日
 6 
 7 change on 07May2017
 8 ‘‘‘
 9 def model(page,per_item,M):
10     try:
11         page = int(page)
12     except:
13         page = 1
14     start = (page-1)*per_item
15     end = page*per_item
16     count = M.objects.all().count()
17     result = M.objects.all()[start:end]
18     print result
19     temp = divmod(count, per_item)
20     if temp[1] == 0 :
21         all_page_count = temp[0]
22     else:
23         all_page_count = temp[0]+1
24     return all_page_count,result,count
25 
26 
27 def pager(page,all_page_count,url):
28     try:
29         page = int(page)
30     except:
31         page = 1
32     #判断如果页面是1,则上一页无,正常上一页为当前少1
33     if page <= 1:
34         page_html = "<a href=http://www.mamicode.com/‘#‘>上一页"
35     else:
36         page_html = "<a href=http://www.mamicode.com/‘%s/%d‘>上一页"%(url,page-1)
37     #首页就是第1页
38     page_html += "<a href=http://www.mamicode.com/‘%s/%d‘>首页"%(url,1) 
39     
40     #页面显示11个页码,如果当前页面少于5,则启始页为1,正常启始页为当前减5
41     if page< 11:
42         page_start = 1
43         page_end = all_page_count+1
44     else:
45         page_start = page-5
46         if all_page_count-6>=page:
47             page_end = page+6
48         else:
49             page_end = all_page_count
50     
51     for i in range(page_start,page_end):
52         if page == i:
53             a_html ="<a class=‘selected‘ href=http://www.mamicode.com/‘%s/%d‘>%d"%(url,i,i)
54         else:
55             a_html ="<a href=http://www.mamicode.com/‘%s/%d‘>%d"%(url,i,i)
56         page_html += a_html
57     
58     page_html += "<a href=http://www.mamicode.com/‘%s/%d‘>尾页"%(url,all_page_count)
59     #
60     if page <= all_page_count-1:
61         page_html += "<a href=http://www.mamicode.com/‘%s/%d‘>下一页"%(url,page+1)
62     else: 
63         page_html += "<a href=http://www.mamicode.com/‘#‘>下一页"
64     page =  mark_safe(page_html)
65     return page

 

html_helper 分页