首页 > 代码库 > Djano 分页 day3 html_helper.py

Djano 分页 day3 html_helper.py

day3:
     将startr 和 end 值加入html_helper.py 更加方便导入
 
  
1:????
     11 个 页码

如果 总页数 < 11
    1-总页数

else:
    当前页 < 6:
        1 - 11

    当前页 > 6:
        如果 当前页 + 5 》 总页数
            当前页 + 5 ---  总页数

        当前页 -5 -- 当前页 + 5

2:输入
    page  当前页数
    num     总页数
    per_item 默认值为5

  输出
     start  
     end
     all_page_count

3:使用类会产生 start 和 end 页数上数据的起始和结尾的; all_page_count:总页数

  

#!/usr/bin/env python
#coding:utf-8
from django.utils.safestring import mark_safe
class PageInfo:
   
    def __init__(self,current_page,all_count,per_item=5):
        self.CurrentPage = current_page
        self.AllCont = all_count
        self.PerItem=per_item
       
       
    @property
    def start(self):   #页数产生开始的数据
        #start = (page-1)*per_item
        return  (self.CurrentPage-1) * self.PerItem
   
    @property
    def end(self):     #页数产生结尾的数据
        #end = page*per_item
        return self.CurrentPage * self.PerItem
   
    @property
    def all_page_count(self):
        """
        tem = divmod(num, per_item) #判断有多少页数,映射再html
        if tem[1] == 0:
            all_page_count = tem[0]
        else:
            all_page_count = tem[0]+1
        """
        tem = divmod(self.AllCont, self.PerItem) #判断有多少页数,映射再html
        if tem[1] == 0:
            all_page_count = tem[0]
        else:
            all_page_count = tem[0]+1
        return all_page_count
   
           
def Pager(page,all_page_count):
    """
    page:当前页数
    all_page_count:总页数
    """
    html_stack = []  #html 上的页数堆积 
    frist_html = "<a href=http://www.mamicode.com/‘/start/index/%d‘>首页" %(1)
    html_stack.append(frist_html)
   
   
    perv_html = "<a href=http://www.mamicode.com/‘/start/index/%d‘>上一页" %(page-1)
    html_stack.append(perv_html)
   
    begin = page - 6
    end = page + 5
   
    if all_page_count < 11:
        begin = 0
        end = all_page_count
    else:
        if page < 6:
            begin = 0
            end = 11
        else:
            if page + 6 > all_page_count:
                begin = page - 6
                end = all_page_count
               
            else:
                begin = page - 6
                end = page + 5
    for i in range(begin,end):
        if page== (i+1):
            a_html = "<a style =‘color:red;‘ href=http://www.mamicode.com/‘/start/index/%d‘>%d" %(i+1,i+1)
        else:
            a_html = "<a href=http://www.mamicode.com/‘/start/index/%d‘>%d" %(i+1,i+1)
        html_stack.append(a_html)
    next_html = "<a href=http://www.mamicode.com/‘/start/index/%d‘>下一页" %(page+1)
    html_stack.append(next_html)       
   
    end_html = "<a href=http://www.mamicode.com/‘/start/index/%d‘>尾页" %(all_page_count)
    html_stack.append(end_html)
   
   
    return mark_safe(‘‘.join(html_stack))#将列表转换为一个大字符串

  按计划做事》》》》》》》》》》》》》》》》

Djano 分页 day3 html_helper.py