码迷,mamicode.com
首页 > 其他好文 > 详细

Django内置分页扩展

时间:2018-06-13 18:06:25      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:exce   return   current   pager   port   --   stomp   pen   tar   

url文件

urlpatterns = [
    path(admin/, admin.site.urls),
    path(index1.html/, views.index1),
]

 

 

views文件

from django.shortcuts import render
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

# Create your views here.

USER_LIST = []
for i in range(1,999):
    temp = {"name":"root"+str(i),"age":i}
    USER_LIST.append(temp)


def index(request):
    per_page_count = 10
    current_page=request.GET.get(p)
    current_page=int(current_page)

    start = (current_page - 1)*per_page_count
    end =current_page*per_page_count
    data = USER_LIST[start:end]

    if current_page<=1:
        prve_pag = 1
    prve_pag = current_page -1
    next_pag = current_page +1
    return render(request,index.html,{"users":data,"prve_page":prve_pag,"next_page":next_pag})


class CustomPaginator(Paginator):
    def __init__(self,current_page,per_page_num,*args,**kwargs):
        super(CustomPaginator,self).__init__(*args,**kwargs)
        #当前页
        self.current_page = int(current_page)
        #最多显示数量
        self.per_page_num = int(per_page_num)

    def per_num_range(self):
        #如果总页数小于最多显示数量
        if self.num_pages<self.per_page_num:
            return range(1,self.num_pages+1)
        part=int(self.per_page_num/2)

        #当前页小于part(页码从领开始)
        if self.current_page <part:
            return range(1,self.per_page_num+1)
        #当前页+part大于总页数(页码最后一位已经显示出来了)
        if (self.current_page+part)>self.num_pages:
            return range(self.num_pages - self.per_pager_num + 1, self.num_pages + 1)
        return range(self.current_page - part, self.current_page + part + 1)

def index1(request):

    # 全部数据:USER_LIST,=》得出共有多少条数据
    # per_page: 每页显示条目数量
    # count:    数据总个数
    # num_pages:总页数
    # page_range:总页数的索引范围,如: (1,10),(1,200)
    # page:     page对象(是否具有下一页;是否有上一页;)
    current_page=request.GET.get(p)
    paginator = CustomPaginator(current_page,7,USER_LIST,10)

    try:
        posts=paginator.page(current_page)
        # has_next              是否有下一页
        # next_page_number      下一页页码
        # has_previous          是否有上一页
        # previous_page_number  上一页页码
        # object_list           分页之后的数据列表,已经切片好的数据
        # number                当前页
        # paginator             paginator对象
    except PageNotAnInteger:
        posts = paginator.page(1)
    except EmptyPage:
        posts=paginator.page((paginator.num_pages))


    return render(request,"index1.html",{"posts":posts})

 

 

html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<ul>
    {% for row in posts.object_list %}
        <li>{{ row.name }}--{{ row.age }}</li>
    {% endfor %}
</ul>
    {% include ‘include/pager.html‘ %}
</body>
</html>

 将分页代码放在include,这样分页功能就可以重用了

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    {% if posts.has_previous %}
        <a href="/index1.html?p={{ posts.previous_page_number }}">上一页</a>
    {% endif %}


    {% for i in posts.paginator.per_num_range %}
        {% if i == posts.number  %}
            <a style="font-size: 30px;" href="/index1.html?p={{ i }}">{{ i }}</a>
        {% else %}
            <a href="/index1.html?p={{ i }}">{{ i }}</a>
        {% endif %}
    {% endfor %}



    {% if posts.has_next%}
        <a href="/index1.html?p={{ posts.next_page_number }}">下一页</a>
    {% endif %}

    <span>{{ posts.number }}/{{ posts.paginator.num_pages}}</span>


</body>
</html>

 

Django内置分页扩展

标签:exce   return   current   pager   port   --   stomp   pen   tar   

原文地址:https://www.cnblogs.com/lhqlhq/p/9178891.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!