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

分页器基本案例思路

时间:2019-10-27 12:21:10      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:path   utf-8   start   label   exception   iis   art   anon   oca   

app01/models.py

from django.db import models

# Create your models here.


class Book(models.Model):
    title=models.CharField(max_length=32)
    price=models.DecimalField(max_digits=6,decimal_places=2)

app01/page.py


import copy


class Pagination(object):
    def __init__(self, current_page, all_count, request, per_page=10, max_pager_num=11):
        try:
            current_page = int(current_page)

        except Exception as e:
            current_page = 1

        if current_page < 1:
            current_page = 1

        self.current_page = current_page
        self.all_count = all_count
        self.per_page = per_page

        num_pages, tmp = divmod(all_count, per_page)
        if tmp:
            num_pages += 1

        self.num_pages = num_pages

        self.max_pager_num = max_pager_num
        self.pager_count_half = int((self.max_pager_num - 1) / 2)

        import copy
        self.parmas = copy.deepcopy(request.GET)

        print("++++++++", self.parmas.urlencode())

    @property
    def start(self):
        return (self.current_page - 1) * self.per_page

    @property
    def end(self):
        return self.current_page * self.per_page

    def page_html(self):
        if self.num_pages <= self.max_pager_num:
            pager_start = 1
            pager_end = self.num_pages + 1

        else:
            if self.current_page <= self.pager_count_half:
                pager_start = 1
                pager_end = self.max_pager_num + 1

            elif (self.current_page + self.pager_count_half) > self.num_pages:
                pager_start = self.num_pages - self.max_pager_num + 1
                pager_end = self.num_pages + 1
            else:
                pager_start = self.current_page - self.pager_count_half
                pager_end = self.current_page + self.pager_count_half + 1

        pager_html_list = []

        # 首页,上一页
        self.parmas["page"]=1
        first_page = <nav aria-label="Page navigation"><ul class="pagination"><li><a href="?%s">首页</a></li> % (
            self.parmas.urlencode(),)
        pager_html_list.append(first_page)
        if self.current_page <= 1:
            prev_page = <li class="disabled"><a href="#">上一页</a></li>
        else:
            self.parmas["page"]=self.current_page-1
            prev_page = prev_page = <li><a href="?%s">上一页</a></li> % (self.parmas.urlencode(),)

        pager_html_list.append(prev_page)

        # 每一页页码

        for i in range(pager_start, pager_end):
            self.parmas["page"] = i
            if i == self.current_page:
                temp = <li class="active"><a href="?%s">%s</a></li> % (self.parmas.urlencode(), i,)
            else:
                temp = <li><a href="?%s">%s</a></li> % (self.parmas.urlencode(), i)

            pager_html_list.append(temp)

        # 最后一页,下一页
        self.parmas["page"] =self.current_page+1
        if self.current_page >= self.num_pages:
            next_page = <li class="disabled"><a href="#">下一页</a></li>
        else:
            next_page = <li><a href="?%s">下一页</a></li> % (self.parmas.urlencode(),)
        pager_html_list.append(next_page)

        self.parmas["page"]=self.num_pages
        last_page = <li><a href="?%s">尾页</a></li></ul></nav> % (self.parmas.urlencode())
        pager_html_list.append(last_page)

        return ‘‘.join(pager_html_list)

 

app01/views.py

def index(request):

    book_list1 = Book.objects.all()
    current_page = request.GET.get("page")
    print(">>",current_page)
    pagination = Pagination(current_page, book_list1.count(),request,per_page=5,max_pager_num=11)
    book_list = book_list1[pagination.start:pagination.end]
    return render(request, "index2.html", locals())

 

templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>分页器</title>

    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
          integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div>
    <ul>

        {% for book in book_list %}

            <li>
                {{ book.title }}-----{{ book.price }}
            </li>
        {% endfor %}

    </ul>

    {{pagination.page_html|safe}}

</div>

</body>
</html>

 

项目总路由/urls.py

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   utf-8   start   label   exception   iis   art   anon   oca   

原文地址:https://www.cnblogs.com/one-tom/p/11746901.html

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