Skip to main content

this project is fork from https://github.com/jamespacileo/django-pure-pagination/ 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!)

Project description

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

Description

Author:

nick

Version:

1.0.0

Description:

this project is fork from https://github.com/jamespacileo/django-pure-pagination/. 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)

Introduction

The django app offers advanced pagination features without forcing major code changes within an existing project.

Django-pure-pagination is based upon Django’s core pagination module and is therefore compatible with the existing api.

Documentation for Django core pagination module

Features

  1. Uses same API as django.core.pagination and therefore is fully compatible with existing code.

  2. Has dynamic query string creation, which takes into consideration existing GET parameters.

  3. Out-of-the-box html rendering of the pagination

  4. Additional methods make it easier to render more advanced pagination templates.

Installation

Install package from PYPI:

pip install nick-test-pagination

or clone and install from repository:

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

Add pure_pagination to INSTALLED_APPS

INSTALLED_APPS = (
    ...
    'pure_pagination',
)

Finally substitute from django.core.paginator import Paginator with from pure_pagination import Paginator

Settings

A few settings can be set within settings.py

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

    'SHOW_FIRST_PAGE_WHEN_INVALID': True,
}

PAGE_RANGE_DISPLAYED is the number of pages neighbouring the current page which will be displayed (default is 10)

MARGIN_PAGES_DISPLAYED is the number of pages neighbouring the first and last page which will be displayed (default is 2)

Set SHOW_FIRST_PAGE_WHEN_INVALID to True when you want to just show first page when provided invalid page instead of 404 error

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

Usage example

Following is a simple example for function based views. For generic class-based views, see bellow.

view file: 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,
    }

template file: 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 %}

Usage

There a few different way you can make use of the features introduced within django-pure-pagination.

Easiest way to render the pagination is to call the render method i.e. {{ page.render }}

Alternatively you can access the Page object low level methods yourself

Special note: page_obj and current_page both point to the page object within the template.

{% 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>

Generic Class-Based Views

Documentation for Django generic class-based views on https://docs.djangoproject.com/en/dev/ref/class-based-views/

view file:

  • 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

template files:

Note that the Django generic-based list view will include the object page_obj in the context. More information on https://docs.djangoproject.com/en/dev/ref/generic-views/#list-detail-generic-views

  • _pagination.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 %}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nick-test-pagination-1.0.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nick_test_pagination-1.0.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file nick-test-pagination-1.0.0.tar.gz.

File metadata

  • Download URL: nick-test-pagination-1.0.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.5

File hashes

Hashes for nick-test-pagination-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3e8fa8a6400f3ac8469f23ce12f41562c2d56712c61b9e9304e82ed0d39f010d
MD5 30aa3e89dc5ca7a37d705b08ab2df229
BLAKE2b-256 0e3da2c61aa7c135a9ae1603057e0114650031d4e53bd83484175a6ad3a0a43b

See more details on using hashes here.

File details

Details for the file nick_test_pagination-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: nick_test_pagination-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.5

File hashes

Hashes for nick_test_pagination-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e5b81c87c41779f646da576ebab783649db8d15286e3ceeda8e21221e5bcca11
MD5 f8e4b0deac929864daa59e75f3cfef3f
BLAKE2b-256 bd69fdc10b49e8474ef962ba453f4776e0bb0f2bdf96fd9664655d1437ef8036

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page