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

13.django之分页

时间:2020-11-01 21:26:57      阅读:23      评论:0      收藏:0      [点我收藏+]

标签:temp   ota   head   条件   gen   param   else   count   ext   

一、手动分页

django从ciew向template传递HTML字符串的时候,django默认不渲染此HTML,为了防止这串字符串里有恶意攻击的代码,所以要使用mark_safe函数

from django.utils.safestring import mark_safe
def view(request):
    pagehtml = makr_safe(‘<a href=‘#‘>首页</a>‘)
    return render(request,‘home.html‘, ‘pagehtml‘: pagehtml)

亦或者直接在前端页面中使用safe过滤器

{% pagehtml|safe %}
page_num = request.GET.get(‘page‘)  # str
    try:
        page_num = int(page_num)
    except Exception:
        # 非数字字符的情况
        page_num = 1

    customers_count = models.Customer.objects.all().count()     # 记录总条数
    # 每页多少条
    per_page_num = 10
    s, y = divmod(customers_count, per_page_num)    # s:每页都显示10条的页数。y:不足10条一页显示

    # 总页码数
    if y:
        page_num_count = s + 1
    else:
        page_num_count = s

    if page_num <= 0:
        page_num = 1
    elif page_num >= page_num_count:
        page_num = page_num_count

    # 页面显示页码数
    page_num_show = 5
    half_show = page_num_show // 2

    if page_num - half_show <= 0:
        start_page_show = 1
        end_page_num = page_num_show + 1
    elif page_num+half_show >= page_num_count:
        start_page_show = page_num_count - page_num_show + 1
        end_page_num = page_num_count + 1
    else:
        start_page_show = page_num - half_show
        end_page_num = page_num + half_show + 1

    page_num_range = range(start_page_show, end_page_num)
    customers_objs = models.Customer.objects.all()[(page_num-1)*per_page_num: page_num*per_page_num]
    print(page_num_range)
    return render(request, ‘saleshtml/customers.html‘, {‘customers_objs‘: customers_objs,
                                                        ‘page_num_range‘: page_num_range})

二、封装后的

import re
from django.utils.safestring import mark_safe


class MyPagenation():
    def __init__(self, page_num, total_count, per_page_num, page_num_show, base_url, get_data=None):
        """
        翻页
        :param page_num: 当前页码
        :param total_count: 总记录数
        :param per_page_num: 每页记录条数
        :param page_num_show: 显示页码数
        :param base_url: 翻页路由
        :param get_data: QueryDict对象
        """
        self.page_num = page_num
        self.per_page_num = per_page_num
        self.base_url = base_url
        self.get_data = get_data    # 搜索条件
        try:
            self.page_num = int(self.page_num)
        except Exception:
            # 非数字字符的情况
            self.page_num = 1

        s, y = divmod(total_count, self.per_page_num)  # s:每页都显示10条的页数。y:不足10条一页显示
        # 总页码数
        if y:
            self.page_num_count = s + 1
        else:
            self.page_num_count = s

        if self.page_num <= 0:
            self.page_num = 1
        elif self.page_num >= self.page_num_count:
            self.page_num = self.page_num_count

        # 页面显示页码数
        half_show = page_num_show // 2

        if self.page_num - half_show <= 0:
            start_page_show = 1
            end_page_num = page_num_show + 1
        elif self.page_num + half_show >= self.page_num_count:
            start_page_show = self.page_num_count - page_num_show + 1
            end_page_num = self.page_num_count + 1
        else:
            start_page_show = self.page_num - half_show
            end_page_num = self.page_num + half_show + 1
        # 如果要显示的页码数小于设定显示的页码总数
        if self.page_num_count < page_num_show:
            start_page_show = 1
            end_page_num = self.page_num_count + 1

        self.start_page_show = start_page_show
        self.end_page_num = end_page_num

    def page_html(self):
        page_num_range = range(self.start_page_show, self.end_page_num)
        # 生成页面html字符串
        page_html = ‘‘
        # 翻页按钮首页
        # first_html = ‘<nav aria-label="Page navigation"><ul class="pagination"><li><a href="{0}?page=1">首页</a></li>‘        #     .format(self.base_url)
        self.get_data[‘page‘] = 1
        first_html = ‘<nav aria-label="Page navigation"><ul class="pagination"><li><a href="{0}?{1}">首页</a></li>‘            .format(self.base_url, self.get_data.urlencode())
        page_html += first_html
        # 上一页的翻页按钮
        # page_pre_html = ‘<li><a href="{0}?page={1}" ‘         #                 ‘aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>‘.        #     format(self.base_url, 1 if self.page_num <= 1 else self.page_num - 1)
        self.get_data[‘page‘] = 1 if self.page_num <= 1 else self.page_num - 1
        page_pre_html = ‘<li><a href="{0}?{1}" ‘                         ‘aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>‘.            format(self.base_url, self.get_data.urlencode())
        page_html += page_pre_html
        # 中间页码按钮
        for foo in page_num_range:
            # if foo == self.page_num:
            #     page_html += ‘<li class="active"><a href="{0}?{1}&page={2}">{3}</a></li>‘            #         .format(self.base_url, re.sub(‘page=\d+‘,‘‘, self.get_data) if ‘page=‘ in self.get_data else self.get_data, foo, foo)
            # else:
            #     page_html += ‘<li><a href="{0}?{1}&page={2}">{3}</a></li>‘             #         .format(self.base_url,re.sub(‘page=\d+‘, ‘‘, self.get_data) if ‘page=‘ in self.get_data else self.get_data, foo, foo)

            if foo == self.page_num:
                self.get_data[‘page‘] = foo
                page_html += ‘<li class="active"><a href="{0}?{1}">{2}</a></li>‘                    .format(self.base_url, self.get_data.urlencode(), foo, foo)
            else:
                self.get_data[‘page‘] = foo
                page_html += ‘<li><a href="{0}?{1}">{2}</a></li>‘                     .format(self.base_url, self.get_data.urlencode(), foo)
        # 下一页
        # page_next_html = ‘<li><a href="{1}?page={0}" aria-label="Next">‘         #                  ‘<span aria-hidden="true">&raquo;</span></a></li>‘.        #     format(self.page_num_count if self.page_num >= self.page_num_count else self.page_num + 1, self.base_url)
        self.get_data[‘page‘] = self.page_num_count if self.page_num >= self.page_num_count else self.page_num + 1
        page_next_html = ‘<li><a href="{0}?{1}" aria-label="Next">‘                          ‘<span aria-hidden="true">&raquo;</span></a></li>‘.             format(self.base_url, self.get_data.urlencode())
        page_html += page_next_html
        # 尾页
        # last_html = ‘<li><a href="{0}?page={1}">尾页</a></li>‘.format(self.base_url, self.page_num_count)
        self.get_data[‘page‘] = self.page_num_count
        last_html = ‘<li><a href="{0}?{1}">尾页</a></li>‘.format(self.base_url, self.get_data.urlencode())
        page_html += last_html
        return mark_safe(page_html)

    @property
    def start_page_data(self):
        return (self.page_num-1) * self.per_page_num

    @property
    def end_page_data(self):
        return self.page_num * self.per_page_num


三、django自带的分页器

。。。

13.django之分页

标签:temp   ota   head   条件   gen   param   else   count   ext   

原文地址:https://www.cnblogs.com/journeyer-xsh/p/13908575.html

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