欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Django实现翻页的示例代码

程序员文章站 2022-12-01 20:31:12
django提供了翻页器。用django的paginator类实现一、views模块导入paginator类实现数据分页apitest/apiviews.py每行都加了注释,希望有助于理解。from...

django提供了翻页器。用django的paginator类实现

一、views模块导入paginator类实现数据分页

apitest/apiviews.py

每行都加了注释,希望有助于理解。

from django.shortcuts import render
from apitest.models import apitest
from django.contrib.auth.decorators import login_required
from django.core.paginator import paginator, emptypage, pagenotaninteger, invalidpage


@login_required
def api_test_manage(request):
    username = request.session.get('user', '')
    # 获取所有接口测试数据
    apitest_list = apitest.objects.all().order_by('id')
    # 生成paginator对象对数据分页,每页显示10条数据
    paginator = paginator(apitest_list,10)
    # 使用request.get.get()函数获取url中的page参数的数值。默认第1页
    page = request.get.get('page', 1)
    # 把获取的当前页码数转换成整数类型
    current_page = int(page)
    try:
     # 获取查询页数的接口数据列表,page()函数会判断page实参是否是有效数字。page()函数源码附在文章的最后
        apitest_list = paginator.page(page)
    except pagenotaninteger:
        apitest_list = paginator.page(1)
    except (emptypage, invalidpage):
     # paginator.num_pages
        apitest_list = paginator.page(paginator.num_pages)
    return render(request, "apitest_manage.html", {'user': username, 'apitests': apitest_list})

二、将views的数据渲染到前端模板上

在对应的html文件中加入此段代码即可。

<div class="container">
    <ul class="pagination" id="pager">
        {# 上一页链接 #}
        {% if apitests.has_previous %}
        <li class="previous"><a href="/apitest_manage/?page={{ apitests.previous_page_number }}" rel="external nofollow" >上一页</a></li>
        {% else %}
        <li class="previous disabled"><a href="#" rel="external nofollow"  rel="external nofollow" >上一页</a></li>
        {% endif %}

        {# 中间数字显示 #}
        {%  for num in apitests.paginator.page_range %}
            {%  if num == currentpage %}
                <li class="item active"><a href="/apitest_manage/?page={{ num }}" rel="external nofollow"  rel="external nofollow" >{{ num }}</a></li>
            {% else %}
                <li class="item"><a href="/apitest_manage/?page={{ num }}" rel="external nofollow"  rel="external nofollow" >{{ num }}</a></li>
            {% endif %}
        {% endfor %}

        {# 下一页链接 #}
        {% if apitests.has_next %}   {# 如果有下一页,则正常显示下一页链接 #}
            <li class="next"><a href="/apitest_manage/?page={{ apitests.next_page_number }}" rel="external nofollow" >下一页</a></li>
        {% else %}
            <li class="next disabled"><a href="#" rel="external nofollow"  rel="external nofollow" >下一页</a></li>
        {% endif %}

    </ul>
</div>

三、效果展示

Django实现翻页的示例代码

四、paginator类源码

此类主要用在views文件

class paginator:
    def __init__(self, object_list, per_page, orphans=0,
                 allow_empty_first_page=true):
        self.object_list = object_list
        self._check_object_list_is_ordered()
        self.per_page = int(per_page)
        self.orphans = int(orphans)
        self.allow_empty_first_page = allow_empty_first_page

    def validate_number(self, number):
        """validate the given 1-based page number."""
        try:
            if isinstance(number, float) and not number.is_integer():
                raise valueerror
            number = int(number)
        except (typeerror, valueerror):
            raise pagenotaninteger(_('that page number is not an integer'))
        if number < 1:
            raise emptypage(_('that page number is less than 1'))
        if number > self.num_pages:
            if number == 1 and self.allow_empty_first_page:
                pass
            else:
                raise emptypage(_('that page contains no results'))
        return number

    def get_page(self, number):
        """
        return a valid page, even if the page argument isn't a number or isn't
        in range.
        """
        try:
            number = self.validate_number(number)
        except pagenotaninteger:
            number = 1
        except emptypage:
            number = self.num_pages
        return self.page(number)

    def page(self, number):
        """return a page object for the given 1-based page number."""
        number = self.validate_number(number)
        bottom = (number - 1) * self.per_page
        top = bottom + self.per_page
        if top + self.orphans >= self.count:
            top = self.count
        return self._get_page(self.object_list[bottom:top], number, self)

    def _get_page(self, *args, **kwargs):
        """
        return an instance of a single page.

        this hook can be used by subclasses to use an alternative to the
        standard :cls:`page` object.
        """
        return page(*args, **kwargs)

    @cached_property
    def count(self):
        """return the total number of objects, across all pages."""
        c = getattr(self.object_list, 'count', none)
        if callable(c) and not inspect.isbuiltin(c) and method_has_no_args(c):
            return c()
        return len(self.object_list)

    @cached_property
    def num_pages(self):
        """return the total number of pages."""
        if self.count == 0 and not self.allow_empty_first_page:
            return 0
        hits = max(1, self.count - self.orphans)
        return ceil(hits / self.per_page)

    @property
    def page_range(self):
        """
        return a 1-based range of pages for iterating through within
        a template for loop.
        """
        return range(1, self.num_pages + 1)

    def _check_object_list_is_ordered(self):
        """
        warn if self.object_list is unordered (typically a queryset).
        """
        ordered = getattr(self.object_list, 'ordered', none)
        if ordered is not none and not ordered:
            obj_list_repr = (
                '{} {}'.format(self.object_list.model, self.object_list.__class__.__name__)
                if hasattr(self.object_list, 'model')
                else '{!r}'.format(self.object_list)
            )
            warnings.warn(
                'pagination may yield inconsistent results with an unordered '
                'object_list: {}.'.format(obj_list_repr),
                unorderedobjectlistwarning,
                stacklevel=3
            )

五、page类源码

此类主要用在html文件

class page(collections.abc.sequence):

    def __init__(self, object_list, number, paginator):
        self.object_list = object_list
        self.number = number
        self.paginator = paginator

    def __repr__(self):
        return '<page %s of %s>' % (self.number, self.paginator.num_pages)

    def __len__(self):
        return len(self.object_list)

    def __getitem__(self, index):
        if not isinstance(index, (int, slice)):
            raise typeerror(
                'page indices must be integers or slices, not %s.'
                % type(index).__name__
            )
        # the object_list is converted to a list so that if it was a queryset
        # it won't be a database hit per __getitem__.
        if not isinstance(self.object_list, list):
            self.object_list = list(self.object_list)
        return self.object_list[index]

    def has_next(self):
        return self.number < self.paginator.num_pages

    def has_previous(self):
        return self.number > 1

    def has_other_pages(self):
        return self.has_previous() or self.has_next()

    def next_page_number(self):
        return self.paginator.validate_number(self.number + 1)

    def previous_page_number(self):
        return self.paginator.validate_number(self.number - 1)

    def start_index(self):
        """
        return the 1-based index of the first object on this page,
        relative to total objects in the paginator.
        """
        # special case, return zero if no items.
        if self.paginator.count == 0:
            return 0
        return (self.paginator.per_page * (self.number - 1)) + 1

    def end_index(self):
        """
        return the 1-based index of the last object on this page,
        relative to total objects found (hits).
        """
        # special case for the last page because there can be orphans.
        if self.number == self.paginator.num_pages:
            return self.paginator.count
        return self.number * self.paginator.per_page

到此这篇关于django实现翻页的示例代码的文章就介绍到这了,更多相关django 翻页内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: Django 翻页