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

Django自定制分页功能

时间:2018-11-25 22:20:06      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:对象   response   ref   ext   color   UNC   pager   home   views   

URL:

"""django_paginner URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path(‘‘, views.home, name=‘home‘)
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path(‘‘, Home.as_view(), name=‘home‘)
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path(‘blog/‘, include(‘blog.urls‘))
"""
from django.contrib import admin
from django.urls import path
from app01 import views

urlpatterns = [
    path(‘admin/‘, admin.site.urls),
    path(‘index/‘, views.index),
    path(‘index1/‘, views.index1),



]

views:

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

# Create your views here.
USER_LIST = []

for i in range(1, 659):
    temp = {‘name‘: ‘root‘ + str(i), ‘age‘: i}
    USER_LIST.append(temp)


def index(request):
    # 当前的页数
    current_page = request.GET.get(‘p‘)
    # 每页显示10个
    per_page_count = 10

    # 第一页(0,10)索引是0到9
    # 第二页(10,20)索引是10到19

    current_page = int(current_page)
    # 开始:当前页-1*每页显示10个
    # 第二页
    # (2-1)*10
    start = (current_page - 1) * per_page_count
    # 2*10
    end = current_page * per_page_count
    data = USER_LIST[start:end]

    if current_page <= 1:
        prev_page = 1
    prev_page = current_page - 1
    next_page = current_page + 1

    return render(request, ‘index.html‘, {‘user_list‘: data, ‘prev_page‘: prev_page, ‘next_page‘: next_page})


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

    def pager_num_range(self):
        # 当前页
        # self.current_page
        # 每页最多能显示的页码数量,漂亮最好取奇数 11
        # self.per_page_num
        # 总页数
        # self.num_pages

        # 总页数是django分页功能根据数据大小自动分配好的
        # 如果总页数小于每页最多能显示的页码数量
        # 那么就返回range(第一页到总页数+1),因为range取值是取(开头,结尾-1)
        if self.num_pages < self.per_page_num:
            return range(1, self.num_pages + 1)

        # 总页数特别多
        # 当你的当前页 小于或等于,最多显示的页数的一半时,显示前五个,后五个
        part = int(self.per_page_num / 2)
        if self.current_page <= part:
            # return range(1, part + 1)
            return range(1, self.per_page_num + 1)
        # 极限值:如果当前页+最多显示页数一半>总页数(超出了显示的范围)
        # 返回range(总页数-最多显示的页数,总页数+1)
        if (self.current_page + part) > self.num_pages:
            return range(self.num_pages-self.per_page_num, self.num_pages + 1)
        # if self.current_page > part:
        return range(self.current_page - part, self.current_page + part + 1)


def index1(request):
    # 全部数据:USER_LIST=得出共有多少条数据
    # 10,
    # 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})

templates-index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <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>

pager.html

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


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

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

{% if posts.has_next %}
    <a href="/index1/?p={{ posts.next_page_number }}">下一页</a>
{% else %}
    <a href="#">下一页</a>
{% endif %}
<span>
            {{ posts.number }}/{{ posts.paginator.num_pages }}
        </span>

</body>
</html>

Django自定制分页功能

标签:对象   response   ref   ext   color   UNC   pager   home   views   

原文地址:https://www.cnblogs.com/ago-0912/p/10017368.html

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