Django之项目分页插件

Django之项目分页插件:

  当一个功能常用的时候,直接把这个功能封装成一个类,放到utils文件里面,需要用的时候直接import出来。

现在项目中分页功能在是必不可少的。所有把它封装组件成组件。

'''
分页组件:
    使用方法:
        from  utils.paper import Pagination
        all_count = models.Host.objects.all().count()
        page_obj = Pagination(request.GET.get('page'),all_count,request.path_info)

        host_list=models.Host.objects.all()[page_obj.start:page_obj.end]
        return  render(request,"host.html",{"page_html":page_obj.page_html,"host_list":host_list})

    HTML:
        <style>
            .pager a{
                display: inline-block;
                padding: 3px 5px;
                margin: 0 3px;
                border: 1px solid #dddddd;
            }
            .pager a.active{
                background-color: cadetblue;
                color: white;
            }
        </style>
        <div class="pager">
            {{ page_html}}
        </div>

'''
# -*- coding: utf-8 -*-
__author__ = 'ShengLeQi'

from django.utils.safestring import mark_safe

class Pagination(object):
    def __init__(self,current_page,total_count,base_url,per_page_count=10,max_page_num=11):
        """
        :param current_page:用户请求当前页
        :param per_page_count:每页显示条数
        :param base_url:基础路径
        :param total_count:数据库中查到的总条数
        :param max_page_num:最多页面上显示的页码
        """

        total_page_count, div = divmod(total_count, per_page_count)
        if div:
            total_page_count +=1
        self.total_page_count=total_page_count

        try:
            current_page=int(current_page)

        except Exception as  e:
            current_page=1

        if current_page>total_page_count:
            current_page=total_page_count

        self.current_page=current_page
        self.per_page_count=per_page_count
        self.base_url=base_url
        self.total_count=total_count
        self.max_page_num=max_page_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 page_html(self):
        page_list = []

        half_max_page_num = int(self.max_page_num / 2)

        # 如果显示的页面数据小于最大页数
        if self.total_page_count <= self.max_page_num:
            page_start = 1
            page_end = self.total_page_count
        else:  # 如果显示的页面数据大于最大页数

            if self.current_page <= self.max_page_num:
                page_start = 1
                page_end = self.max_page_num
            else:
                if (self.current_page + 5) >= self.total_page_count:
                    page_start = self.total_page_count - 11
                    page_end = self.total_page_count
                else:
                    page_start = self.current_page - half_max_page_num  # 当前页-5
                    page_end = self.current_page + half_max_page_num  # 当前页+5

        # 上一页
        if self.current_page <= 1:
            prev = "<a href='#'>上一页</a>"
        else:
            prev = "<a href='%s?page=%s'>上一页</a>" % (self.base_url,self.current_page - 1,)
        page_list.append(prev)

        for i in range(page_start, page_end + 1):
            if i == self.current_page:  # 当点击当前页
                tag = "<a class='active' href='%s?page=%s'>%s</a>" % (i, i,)
            else:
                tag = "<a href='%s?page=%s'>%s</a>" % (self.base_url,i, i,)
            page_list.append(tag)

        # 下一页
        if self.current_page >= self.total_page_count:
            nex = "<a href='#'>下一页</a>"
        else:
            nex = "<a href='%s?page=%s'>下一页</a>" % (self.base_url,self.current_page + 1,)
        page_list.append(nex)

        return  mark_safe("".join(page_list))

Python代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
            margin: 0;
        }
        .pg-header{
            height: 48px;
            background-color: cadetblue;
        }
    </style>

    {% block css %}{% endblock%}
</head>
<body>
<div class="pg-header">菜单</div>

{% block body %}{%  endblock %}

<script></script>
{% block js %}{% endblock %}

</body>
</html>
layout.htm
{% extends "layout.html" %}

{% block css %}
    <style>
        .pager a{
            display: inline-block;
            padding: 3px 5px;
            margin: 0 3px;
            border: 1px solid #dddddd;
        }
        .pager a.active{
            background-color: cadetblue;
            color: white;
        }
    </style>
{% endblock css %}

{% block body %}
        <table>
            <thead>
                <tr>
                    <th>hostname</th>
                    <th>ip</th>
                    <th>port</th>
                </tr>
            </thead>
            {% for host in host_list %}
                <tr>
                    <td>{{ host.hostname }}</td>
                    <td>{{ host.ip }}</td>
                    <td>{{ host.port }}</td>
                </tr>
            {% endfor %}
        </table>
    <div class="pager">
        {{ page_html|safe}}
    </div>
{% endblock body %}
host.html
'''
分页组件:
    使用方法:
        from  utils.paper import Pagination
        all_count = models.Host.objects.all().count()
        page_obj = Pagination(request.GET.get('page'),all_count,request.path_info)

        host_list=models.Host.objects.all()[page_obj.start:page_obj.end]
        return  render(request,"host.html",{"page_html":page_obj.page_html,"host_list":host_list})

    HTML:
        <style>
            .pager a{
                display: inline-block;
                padding: 3px 5px;
                margin: 0 3px;
                border: 1px solid #dddddd;
            }
            .pager a.active{
                background-color: cadetblue;
                color: white;
            }
        </style>
        <div class="pager">
            {{ page_html}}
        </div>

'''
# -*- coding: utf-8 -*-
__author__ = 'ShengLeQi'

from django.utils.safestring import mark_safe

class Pagination(object):
    def __init__(self,current_page,total_count,base_url,per_page_count=10,max_page_num=11):
        """
        :param current_page:用户请求当前页
        :param per_page_count:每页显示条数
        :param base_url:基础路径
        :param total_count:数据库中查到的总条数
        :param max_page_num:最多页面上显示的页码
        """

        total_page_count, div = divmod(total_count, per_page_count)
        if div:
            total_page_count +=1
        self.total_page_count=total_page_count

        try:
            current_page=int(current_page)

        except Exception as  e:
            current_page=1

        if current_page>total_page_count:
            current_page=total_page_count

        self.current_page=current_page
        self.per_page_count=per_page_count
        self.base_url=base_url
        self.total_count=total_count
        self.max_page_num=max_page_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 page_html(self):
        page_list = []

        half_max_page_num = int(self.max_page_num / 2)

        # 如果显示的页面数据小于最大页数
        if self.total_page_count <= self.max_page_num:
            page_start = 1
            page_end = self.total_page_count
        else:  # 如果显示的页面数据大于最大页数

            if self.current_page <= self.max_page_num:
                page_start = 1
                page_end = self.max_page_num
            else:
                if (self.current_page + 5) >= self.total_page_count:
                    page_start = self.total_page_count - 11
                    page_end = self.total_page_count
                else:
                    page_start = self.current_page - half_max_page_num  # 当前页-5
                    page_end = self.current_page + half_max_page_num  # 当前页+5

        # 上一页
        if self.current_page <= 1:
            prev = "<a href='#'>上一页</a>"
        else:
            prev = "<a href='%s?page=%s'>上一页</a>" % (self.base_url,self.current_page - 1,)
        page_list.append(prev)

        for i in range(page_start, page_end + 1):
            if i == self.current_page:  # 当点击当前页
                tag = "<a class='active' href='/host/?page=%s'>%s</a>" % (i, i,)
            else:
                tag = "<a href='%s?page=%s'>%s</a>" % (self.base_url,i, i,)
            page_list.append(tag)

        # 下一页
        if self.current_page >= self.total_page_count:
            nex = "<a href='#'>下一页</a>"
        else:
            nex = "<a href='%s?page=%s'>下一页</a>" % (self.base_url,self.current_page + 1,)
        page_list.append(nex)

        return  mark_safe("".join(page_list))
paper.py

使用方法:

"""
分页组件:
    使用方法:
        视图函数:
            from utils.pager import Pagination
            def host(request):
                all_count = models.Host.objects.all().count()
                # page_obj = Pagination(request.GET.get('page'),all_count,'/host/')
                page_obj = Pagination(request.GET.get('page'),all_count,request.path_info)
                host_list = models.Host.objects.all()[page_obj.start:page_obj.end]
                return render(request,'host.html',{'host_list':host_list,'page_html':  page_obj.page_html()})
        HTML:
            <style>
                .pager a{
                    display: inline-block;
                    padding: 3px 5px;
                    margin: 0 3px;
                    border: 1px solid #dddddd;
                }
                .pager a.active{
                    background-color: cadetblue;
                    color: white;
                }

            </style>

            <div class="pager">
                {{ page_html}}
            </div>

"""

 

posted @ 2017-12-29 15:18  ShengLeQi  阅读(95)  评论(0)    收藏  举报