django纯分页提供了高级分页功能,并且与基于django核心分页模块的现有代码完全兼容。(又名不需要重写代码!)

django-pure-pagination的Python项目详细描述


https://travis-ci.org/hovel/django-pure-pagination.svg?branch=master

说明

Author:James Pacileo @ignighted
Version:0.3.0
Description:django-pure-pagination provides advanced pagination features and is fully compatible with existing code based on Django’s core pagination module. (aka no need to rewrite code!)
Requirements:Django 1.7+
Contributors:juandecarrion (Juande Carrion), twidi (Stéphane Angel), bebraw (Juho Vepsäläinen), lampslave (), GeyseR (Sergey Fursov), zeus (Pavel Zhukov)

简介

Django应用程序提供了高级分页功能,而不强制在现有项目中更改主要代码。

django纯分页是基于Django的核心分页模块,因此与现有API兼容。

Documentation for Django core pagination module

功能

    使用相同的API作为^ {STR 1 } $django.Cyr.PigItP<强>,因此与现有代码完全兼容。具有动态查询字符串创建,它考虑到现有的获取参数。
  1. 开箱即用HTML呈现分页
  2. 其他方法使呈现更高级的分页模板更容易。

安装

从pypi安装包:

pip install django-pure-pagination

或从存储库克隆并安装:

git clone git@github.com:jamespacileo/django-pure-pagination.git
cd django-pure-pagination
python setup.py install

将纯分页添加到已安装的应用程序中

INSTALLED_APPS = (
    ...
    'pure_pagination',
)

最后将django.core.paginator import paginator的替换为纯分页import paginator的

设置

可以在settings.py中设置一些设置

PAGINATION_SETTINGS = {
    'PAGE_RANGE_DISPLAYED': 10,
    'MARGIN_PAGES_DISPLAYED': 2,

    'SHOW_FIRST_PAGE_WHEN_INVALID': True,
}

page_range_displayed是将要显示的与当前页相邻的页数(默认值为10)

margin_pages_displayed是将显示的第一页和最后一页相邻的页数(默认值为2)

设置show_first_page_when_invalid为true,以便在提供无效页时仅显示第一页,而不是404错误

http://i.imgur.com/LCqrt.gif

使用示例

下面是基于函数的视图的一个简单示例。有关基于类的通用视图,请参见下文。

查看文件:views.py

# views.py
from django.shortcuts import render_to_response

from pure_pagination import Paginator, EmptyPage, PageNotAnInteger


def index(request):

    try:
        page = request.GET.get('page', 1)
    except PageNotAnInteger:
        page = 1

    objects = ['john', 'edward', 'josh', 'frank']

    # Provide Paginator with the request object for complete querystring generation

    p = Paginator(objects, request=request)

    people = p.page(page)

    return render_to_response('index.html', {
        'people': people,
    }

模板文件:index.html

{# index.html #}
{% extends 'base.html' %}

{% block content %}

{% for person in people.object_list %}
    <div>
        First name: {{ person }}
    </div>
{% endfor %}

{# The following renders the pagination html #}
<div id="pagination">
    {{ people.render }}
</div>

{% endblock %}

使用量

有几种不同的方法可以利用django pure pagination中引入的特性。

呈现分页的最简单方法是调用呈现方法,即{{page.render}

或者,您也可以自己访问页面对象低级方法

特别注意:页面对象当前页面都指向模板中的页面对象。

{% load i18n %}
<div class="pagination">
    {% if page_obj.has_previous %}
        <a href="?{{ page_obj.previous_page_number.querystring }}" class="prev">&lsaquo;&lsaquo; {% trans "previous" %}</a>
    {% else %}
        <span class="disabled prev">&lsaquo;&lsaquo; {% trans "previous" %}</span>
    {% endif %}
    {% for page in page_obj.pages %}
        {% if page %}
            {% ifequal page page_obj.number %}
                <span class="current page">{{ page }}</span>
            {% else %}
                <a href="?{{ page.querystring }}" class="page">{{ page }}</a>
            {% endifequal %}
        {% else %}
            ...
        {% endif %}
    {% endfor %}
    {% if page_obj.has_next %}
        <a href="?{{ page_obj.next_page_number.querystring }}" class="next">{% trans "next" %} &rsaquo;&rsaquo;</a>
    {% else %}
        <span class="disabled next">{% trans "next" %} &rsaquo;&rsaquo;</span>
    {% endif %}
</div>

基于类的通用视图

django基于类的视图的文档https://docs.djangoproject.com/en/dev/ref/class-based-views/

查看文件:

  • views.py

    # views.py
    from django.views.generic import ListView
    
    from pure_pagination.mixins import PaginationMixin
    
    from my_app.models import MyModel
    
    
    class MyModelListView(PaginationMixin, ListView):
        # Important, this tells the ListView class we are paginating
        paginate_by = 10
    
        # Replace it for your model or use the queryset attribute instead
        object = MyModel
    

模板文件:

请注意,基于django泛型的列表视图将在上下文中包含对象page_obj。有关https://docs.djangoproject.com/en/dev/ref/generic-views/#list-detail-generic-views的详细信息

  • _分页.html

    {% load i18n %}
    <div class="pagination">
        {% if page_obj.has_previous %}
            <a href="?{{ page_obj.previous_page_number.querystring }}" class="prev">&lsaquo;&lsaquo; {% trans "previous" %}</a>
        {% else %}
            <span class="disabled prev">&lsaquo;&lsaquo; {% trans "previous" %}</span>
        {% endif %}
        {% for page in page_obj.pages %}
            {% if page %}
                {% ifequal page page_obj.number %}
                    <span class="current page">{{ page }}</span>
                {% else %}
                    <a href="?{{ page.querystring }}" class="page">{{ page }}</a>
                {% endifequal %}
            {% else %}
                ...
            {% endif %}
        {% endfor %}
        {% if page_obj.has_next %}
            <a href="?{{ page_obj.next_page_number.querystring }}" class="next">{% trans "next" %} &rsaquo;&rsaquo;</a>
        {% else %}
            <span class="disabled next">{% trans "next" %} &rsaquo;&rsaquo;</span>
        {% endif %}
    </div>
    
  • my_app/myobject_list.html

    {# my_app/myobject_list.html #}
    {% extends 'base.html' %}
    
    {% block content %}
    
    {% for object in object_list %}
        <div>
            First name: {{ object.first_name }}
        </div>
    {% endfor %}
    
    {# The following renders the pagination html #}
    {% include "_pagination.html" %}
    
    {% endblock %}
    

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java获取JEditorPane中字符的绝对位置   java Datetime:将时间段拆分为天、小时和分钟   java是使此HashMap更高效的一种方法   java项目reactor:collectList()之后的block()对Flux不起作用。创建()   java在Mac OSX上安装OpenCV   java递归地确定一组数字是否包含两个总和相等的子集   Quad2D曲线上的几何图形Java绘图箭头   java将SSL证书导入Glassfish 4。十、   java Android未找到处理Intent MediaScanner的活动   EclipseJava。安全cert.CertificateParsingException:java。木卫一。IOException:主题密钥,无法创建EC公钥   java我能在O(M log N)时间内完成吗?   java跟踪eclipse中的资源更改也在中。元数据和。项目   java如何完全禁用Android键盘   java返回到上一个活动