1. 程式人生 > >html_helper 分頁

html_helper 分頁

mode import 顯示 頁碼 model 正常 ret blog chang

 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=‘#‘>上一頁</a>" 35 else: 36 page_html = "<a href=‘%s/%d‘>上一頁</a>"%(url,page-1)
37 #首頁就是第1頁 38 page_html += "<a href=‘%s/%d‘>首頁</a>"%(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=‘%s/%d‘>%d</a>"%(url,i,i) 54 else: 55 a_html ="<a href=‘%s/%d‘>%d</a>"%(url,i,i) 56 page_html += a_html 57 58 page_html += "<a href=‘%s/%d‘>尾頁</a>"%(url,all_page_count) 59 # 60 if page <= all_page_count-1: 61 page_html += "<a href=‘%s/%d‘>下一頁</a>"%(url,page+1) 62 else: 63 page_html += "<a href=‘#‘>下一頁</a>" 64 page = mark_safe(page_html) 65 return page

html_helper 分頁