django分页

django分页四

2017-07-18  本文已影响0人  赖三石

在app01下新建utils包,之中创建pagination.py

# encoding=utf-8
__author__ = 'my'


class Page(object):

    def __init__(self, current_page, data_count, per_page_count=8, view_page=11):
        self.current_page = current_page
        self.data_count = data_count
        self.per_page_count = per_page_count
        self.view_page = view_page

    @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_num(self):
        v, y = divmod(self.data_count, self.per_page_count)
        #y为余数,y=0为假
        if y:
            v += 1
        return v

    def page_html_method(self):
#本来应该self.page_num(),但是有了@property修饰,可以self.page_num取得.
        if self.page_num <= (self.view_page+1)/2:
            start_index = 1
            end_index = self.page_num + 1
        else:
            if self.current_page - (self.view_page-1)/2 <= 0:
                start_index = 1
                end_index = self.current_page + (self.view_page-1)/2 + 1
            else:
                if self.current_page + (self.view_page-1)/2 + 1 > self.page_num:
                    end_index = self.page_num + 1
                    start_index = self.page_num - self.view_page + 1
                else:
                    start_index = self.current_page - (self.view_page-1)/2
                    end_index = self.current_page + (self.view_page-1)/2 + 1


        page_html_list = []

        if self.current_page == 1:
            prev = '<a href="#">上一页</a>'
        else:
            prev = '<a href="/index/?p=%s">上一页</a>' % (self.current_page-1)
        page_html_list.append(prev)

        for j in range(start_index, end_index):
            if self.current_page == j:
                page_html = '<a class="active" href="/index/?p=%s">%s</a>' % (j, j)
            else:
                page_html = '<a href="/index/?p=%s">%s</a>' % (j, j)
            page_html_list.append(page_html)

        if self.current_page == self.page_num:
            nex = '<a href="#">下一页</a>'
        else:
            nex = '<a href="/index/?p=%s">下一页</a>' % (self.current_page+1)
        page_html_list.append(nex)

        return page_html_list

views.py

# encoding=utf-8
from django.shortcuts import render
from app01.utils import pagination

def index(request):
    #模拟数据,列表生成式
    a_list = [i for i in range(1, 1003)]

    current_page = request.GET.get('p', 1)
    current_page = int(current_page)

    data_count = len(a_list)

    p = pagination.Page(current_page, data_count)
    #  data_count为数据总个数
    data = a_list[p.start: p.end]
    html = p.page_html_method()

    return render(request, 'index.html', {'data': data, 'page': html})

template

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .active{
            background-color: #1b6d85;
        }
    </style>
</head>
<body>

<ul>
    {% for d in data %}
        <li>{{ d }}</li>
    {% endfor %}
</ul>

{% for i in page %}
{{ i|safe }}
{% endfor %}
</body>
</html>
上一篇下一篇

猜你喜欢

热点阅读