Django分页器(类实现)

安装tree:yum install tree

使用tree:最简单的使用办法是直接输入tree命令,就会自动给我们以树形的形式列出当前目录的文件和文件夹,不加任何参数,它会自动列表当前目录下面所有深度级别的文件和目录。
只查看当前第一级的目录和文件:"tree -L 1 ";
只查看当前第二级的目录和文件:"tree -L 2 ";
只查看当前第N级的目录和文件:"tree -L N ";
保存目录结构信息:tree -L 2 > /home/xyh/tree.txt;

Pagination(类实现)

目录结构

 

具体实现代码

-----------------------models.py-----------------------
from django.db import models

class UserInfo(models.Model):
    name = models.CharField(max_length=32)
    pwd = models.CharField(max_length=32)
 

 

----------------tools之pagination.py----------------

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "kaylee"
# Date: 2017/9/20
'''
使用方法示例:
from app01.tools.pagination import Pagination
def user(request):
    user_list=models.UserInfo.objects.all()
    total_record_count=user_list.count()
    current_page_num=int(request.GET.get("page",1))
    base_url='/users.html/'

    page_obj = Pagination(user_list,total_record_count, current_page_num,base_url)
    per_page_records=page_obj.show_records
    total_page_html=page_obj.total_page_html

    return render(request,"user.html",{"per_page_records":per_page_records,
                                       "total_page_html":total_page_html})
'''

from django.utils.safestring import mark_safe
class Pagination:
    def __init__(self, obj,total_record_count, current_page_num,base_url,
                 page_count=11, per_records_num=15):
        '''
        
        :param obj: Queryset,所有用户对象的集合
        :param total_record_count: 用户表的总记录条数
        :param current_page_num: 当前页码
        :param base_url: 要应用的URL
        :param page_count: 指定显示页码个数
        :param per_records_num: 每页记录条数
        '''
        self.obj=obj
        self.total_record_count = total_record_count
        self.current_page_num = current_page_num
        self.base_url=base_url
        self.per_records_num = per_records_num
        max_page_num, b = divmod(total_record_count, per_records_num)
        if b:
            max_page_num = max_page_num + 1
        self.max_page_num = max_page_num
        self.page_count = page_count
        half_page_count = int(page_count / 2)
        self.half_page_count = half_page_count

    def start(self):
        '''
        起始页码
        :return: 
        '''
        if self.current_page_num <= self.half_page_count + 1:
            return 1
        elif self.current_page_num >= self.max_page_num - self.half_page_count:
            return self.max_page_num - self.half_page_count * 2
        else:
            return self.current_page_num - self.half_page_count

    def stop(self):
        '''
        结束页码
        :return: 
        '''
        if self.current_page_num <= self.half_page_count + 1:
            return self.page_count + 1
        elif self.current_page_num >= self.max_page_num - self.half_page_count:
            return self.max_page_num + 1
        else:
            return self.current_page_num + self.half_page_count + 1

    @property
    def show_records(self):
        '''
        当前页码的所有记录,一个个对象
        :return: 
        '''
        record_start = (self.current_page_num - 1) * self.per_records_num
        record_stop = self.current_page_num * self.per_records_num
        per_page_records=self.obj[record_start:record_stop]
        return per_page_records

    @property
    def total_page_html(self):
        '''
        页码显示
        :return: 
        '''
        page_num_list = []

        # 如果当前是第一页,上一页禁止点击,上一页跳转页码为1
        if self.current_page_num == 1:
            prev_page_html = '<li class="disabled"><a href="%s?page=1">上一页</a></li>'%self.base_url
        else:
            prev_page_html = '<li><a href="%s?page=%s">上一页</a></li>' % (self.base_url,self.current_page_num - 1)        page_num_list.append(prev_page_html)        for page_num in range(self.start(), self.stop()):            if page_num == self.current_page_num:                page_html = '<li class="active"><a href="%s?page=%s">%s</a></li>' % (self.base_url,page_num, page_num)            else:                page_html = '<li><a href="%s?page=%s">%s</a></li>' % (self.base_url,page_num, page_num)            page_num_list.append(page_html)        if self.current_page_num == self.max_page_num:            next_page_html = '<li class="disabled"><a href="%s?page=%s">下一页</a></li>' % (self.base_url,self.max_page_num)        else:            next_page_html = '<li><a href="%s?page=%s">下一页</a></li>' % (self.base_url,self.current_page_num + 1)        page_num_list.append(next_page_html)        return mark_safe(''.join(page_num_list))

 

---------------------views.py---------------------

from django.shortcuts import render
from app01 import models
from app01.tools.pagination import Pagination

def user(request):
    '''
    创建表记录
    # for i in range(1,302):
    #     models.UserInfo.objects.create(name='kaylee%s'%i,pwd='123%s'%i)
    :param request: 
    :return: 
    '''

    user_list=models.UserInfo.objects.all()
    total_record_count=user_list.count()
    current_page_num=int(request.GET.get("page",1))
    base_url='/users.html/'

    page_obj = Pagination(user_list,total_record_count, current_page_num,base_url)
    per_page_records=page_obj.show_records
    total_page_html=page_obj.total_page_html


    return render(request,"user.html",{"per_page_records":per_page_records,
                                       "total_page_html":total_page_html})

 

--------------------settings.py--------------------
#设置
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR,'app01/static') ,
)

 

-----------------urls.py-----------------

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^users.html',views.user),
]

 

------------------------templates之user.html------------------------

{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="{% static 'bootstrap/css/bootstrap.css' %}">
</head>
<body>
<div class="container">
    <div class="row">
        <table class="table table-bordered table-hover">
            <thead>
            <tr>
                <th>ID</th>
                <th>用户名</th>
                <th>密码</th>
            </tr>
            </thead>
            <tbody>
            {% for record in per_page_records %}
                <tr>
                    <td>{{ record.id }}</td>
                    <td>{{ record.name }}</td>
                    <td>{{ record.pwd }}</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
    <ul class="pagination">
        {{ total_page_html }}
    </ul>
</div>
</body>
</html>

效果图

 

posted @ 2017-09-30 21:45  始怡  阅读(121)  评论(0)    收藏  举报