zoukankan      html  css  js  c++  java
  • Django 分页功能

                Django 分页功能比较强大,这边是结合官网的内容写的可以参考

    https://docs.djangoproject.com/en/1.9/topics/pagination/

    分页命令行练习案列

    >>> from django.core.paginator import Paginator
    >>> objects = ['john', 'paul', 'george', 'ringo']
    >>> p = Paginator(objects, 2)
    
    >>> p.count
    4
    >>> p.num_pages
    2
    >>> type(p.page_range)  # `<type 'rangeiterator'>` in Python 2.
    <class 'range_iterator'>
    >>> p.page_range
    range(1, 3)
    
    >>> page1 = p.page(1)
    >>> page1
    <Page 1 of 2>
    >>> page1.object_list
    ['john', 'paul']
    
    >>> page2 = p.page(2)
    >>> page2.object_list
    ['george', 'ringo']
    >>> page2.has_next()
    False
    >>> page2.has_previous()
    True
    >>> page2.has_other_pages()
    True
    >>> page2.next_page_number()
    Traceback (most recent call last):
    ...
    EmptyPage: That page contains no results
    >>> page2.previous_page_number()
    1
    >>> page2.start_index() # The 1-based index of the first item on this page
    3
    >>> page2.end_index() # The 1-based index of the last item on this page
    4
    
    >>> p.page(0)
    Traceback (most recent call last):
    ...
    EmptyPage: That page number is less than 1
    >>> p.page(3)
    Traceback (most recent call last):
    ...
    EmptyPage: That page contains no results
    

    views

    from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
    from django.shortcuts import render
    
    def listing(request):
        contact_list = Contacts.objects.all()
        paginator = Paginator(contact_list, 25) # Show 25 contacts per page
    
        page = request.GET.get('page')
        try:
            contacts = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            contacts = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of results.
            contacts = paginator.page(paginator.num_pages)
    
        return render(request, 'list.html', {'contacts': contacts})
    

      

    网页html 代码编写

    {% for contact in contacts %}
        {# Each "contact" is a Contact model object. #}
        {{ contact.full_name|upper }}<br />
        ...
    {% endfor %}
    
    <div class="pagination">
        <span class="step-links">
            {% if contacts.has_previous %}
                <a href="?page={{ contacts.previous_page_number }}">previous</a>
            {% endif %}
    
            <span class="current">
                Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.
            </span>
    
            {% if contacts.has_next %}
                <a href="?page={{ contacts.next_page_number }}">next</a>
            {% endif %}
        </span>
    </div>
    
  • 相关阅读:
    ASP.NET操作DataTable各种方法总结(给Datatable添加行列、DataTable选择排序等)
    SharePoint服务器端对象模型 之 使用CAML进展数据查询
    django-rest_framwork 序列化
    django模型中的抽象类
    微博与本地用户绑定
    微博三方登陆--回调接口
    微博三方登陆开发平台新建APP
    生成微博授权的url(二维码登陆页面)
    博客第三方登陆原理
    redis的pipeline使用
  • 原文地址:https://www.cnblogs.com/yexiaochong/p/5826905.html
Copyright © 2011-2022 走看看