django自定义分页分页类

django中封装了自定义分页的功能,但是自己怎么用,怎么感觉不爽,于是是自己就查阅资料,再耗时了几天后,终于整理出了

自己的分页类,在这里分享给大家,当然代码中带有不足,大家多多见谅。。  声明在py2    要是在py3中死都不知道怎么死的....

最后的效果

 

1.首先建立  PageClass.py

#coding:utf-8
from django.utils.safestring import mark_safe


class PageInfo(object):
    # current_page 当前页
    # all_count 总条数
    # per_item  每页的条数
    # page_count显示的页码数

    def __init__(self,current_page,all_count,per_item=5,page_count=5):
        try:
            current_page = int(current_page)
        except Exception as e:
            current_page = 1
        self.CurrentPage=current_page
        self.AllCount=all_count
        self.PerItem=per_item
        self.PageCount = page_count

    @property
    def start(self):
        return (self.CurrentPage-1)*self.PerItem

    @property
    def end(self):
        return self.CurrentPage*self.PerItem

    @property
    def all_page_count(self):
        temp = divmod(self.AllCount, self.PerItem)
        if temp[1]==0:
            all_page_num = temp[0]
        else:
            all_page_num = temp[0]+1
        return all_page_num

    def page_str(self,base_url):
        # base_url页面访问的路径
        page_html = []
        first = "<a class='action' href='%s%d'>首页</a>" % (base_url,1)
        page_html.append(first)
        if self.CurrentPage <= 1:
            prev = "<a class='action' href='javascript:void(0);'>上一页</a>"
        else:
            prev = "<a class='action' href='%s%d'>上一页</a>" % (base_url,self.CurrentPage - 1)
        page_html.append(prev)

        if self.all_page_count <self.PageCount+1:
            begin = 0
            end = self.all_page_count
        # 总页数大于设置的页数
        else:
            if self.CurrentPage < (self.PageCount+1)/2:
                begin =0
                end = self.PageCount
            else:
                if self.CurrentPage+5 > self.all_page_count:
                    begin = self.CurrentPage-(self.PageCount+1)/2
                    end = self.all_page_count
                else:
                    begin = self.CurrentPage - (self.PageCount+1)/2
                    end = self.CurrentPage + (self.PageCount-1)/2

        for i in range(begin,end):
            if self.CurrentPage == (i + 1):
                a_html = "<a class='action page' href='%s%d'>%d</a>" % (base_url,i + 1, i + 1)
            else:
                a_html = "<a class='action' href='%s%d'>%d</a>" % (base_url,i + 1, i + 1)
            page_html.append(a_html)
        if self.CurrentPage == self.all_page_count:
            next = "<a class='action' href='javascript:void(0);'>下一页</a>"
        else:
            next = "<a class='action' href='%s%d'>下一页</a>" % (base_url,self.CurrentPage + 1,)
        page_html.append(next)
        last = "<a class='action' href='%s%d'>尾页</a>" % (base_url,self.all_page_count,)
        page_html.append(last)
        page_string = mark_safe("".join(page_html))
        return page_string

 在views.py中  

from app01 import PageClass

def pages(request,pid):    

    count = models.Host.objects.all().count()
    page = PageClass.PageInfo(pid,count)
    data = models.Host.objects.all()[page.start:page.end]
    page_str = page.page_str('/zuhe/pages/')
    ret = {'data':data,'count':count,'page':page_str}
    return  render(request,'zuhe/pages.html',ret)

模型中只是一堆的数据就不显示了

 

在html页面中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .action{
            background: #084;text-decoration: none;display: inline-block;border: 1px solid #078;
            color: #fff;padding: 5px;margin: 3px;}
        .page{color: red;background:#080;}
    </style>
</head>
<body>
<ul>
{% for v in data %}
    <li>{{ v.id }}--{{ v.HostName }} -- {{ v.HostIp }}</li>
{% endfor %}
<h4>总数:{{ count }}</h4>
{{ page }}
</ul>
</body>
</html>

在html中只是需要对样式的修改即可

 

posted @ 2017-06-06 15:24  严恩娜  阅读(287)  评论(0编辑  收藏  举报