jmcclell / django-bootstrap-pagination

Django template tag for rendering Page objects as Bootstrap pagination HTML
MIT License
212 stars 83 forks source link
bootstrap bootstrap-3 bootstrap-4 bootstrap-pagination django python

Project No Longer Maintained

This project lived a long and useful life, but it's been inactive for quite some time and I believe its relevancy has faded.

It will remain here on Github in a read-only, archived state but there will be no more updates.

PyPi version PyPi downloads Build Status

Django Bootstrap Pagination

Bootstrap Compatibility

Versions Bootstrap Versions Notes
< 1.1.0 2.x
> 1.1.0, < 1.7.0 3.x
>= 1.7.0 3.x, 4.x bootstrap_pager is only compatible with Bootstrap 3.x

This application serves to make using Twitter's Bootstrap Pagination styles work seamlessly with Django Page objects. By passing in a Page object and one or more optional arguments, Bootstrap pagination bars and pagers can be rendered with very little effort.

Compatible with Django 1.2+

Installation

PIP

This will install the latest stable release from PyPi.

    pip install django-bootstrap-pagination

Download

Download the latest stable distribution from:

http://pypi.python.org/pypi/django-bootstrap-pagination

Download the latest development version from:

github @ http://www.github.com/jmcclell/django-bootstrap-pagination

    setup.py install

Usage

Setup

Make sure you include bootstrap_pagination in your installed_apps list in settings.py:

    INSTALLED_APPS = (
        'bootstrap_pagination',
    )

Additionally, include the following snippet at the top of any template that makes use of the pagination tags:

    {% load bootstrap_pagination %}

Finally, make sure that you have the request context processor enabled:

    # Django 1.8+
    TEMPLATES = [
        {
            # ...
            'OPTIONS': {
                context_processors': [
                    # ...
                    'django.template.context_processors.request',
                ]
            }
        }
    ]

    # Django < 1.8
    TEMPLATE_CONTEXT_PROCESSORS = {
        "django.core.context_processors.request",
    )

bootstrap_paginate

All Optional Arguments

Basic Usage

The following will show a pagination bar with a link to every page, a previous link, and a next link:

    {% bootstrap_paginate page_obj %}

The following will show a pagination bar with at most 10 page links, a previous link, and a next link:

    {% bootstrap_paginate page_obj range=10 %}

The following will show a pagination bar with at most 10 page links, a first page link, and a last page link:

    {% bootstrap_paginate page_obj range=10 show_prev_next="false" show_first_last="true" %}

Advanced Usage

Given a url configured such as:

    archive_index_view = ArchiveIndexView.as_view(
        date_field='date',
        paginate_by=10,
        allow_empty=True,
        queryset=MyModel.all(),
        template_name='example/archive.html'
    )

    urlpatterns = patterns(
        'example.views',
        url(r'^$', archive_index_view, name='archive_index'),
        url(r'^page/(?P<page>\d+)/$', archive_index_view,
            name='archive_index_paginated'))

We could simply use the basic usage (appending ?page=#) with the archive_index URL above, as the archive_index_view class based generic view from django doesn't care how it gets the page parameter. However, if we want pretty URLs, such as those defined in the archive_index_paginated URL (ie: /page/1), we need to define the URL in our template tag:

    {% bootstrap_paginate page_obj url_view_name="archive_index_paginated" %}

Because we are using a default page parameter name of "page" and our URL requires no other parameters, everything works as expected. If our URL required additional parameters, we would pass them in using the optional arguments url_extra_args and url_extra_kwargs. Likewise, if our page parameter had a different name, we would pass in a different url_param_name argument to the template tag.

bootstrap_pager

A much simpler implementation of the Bootstrap Pagination functionality is the Pager, which simply provides a Previous and Next link.

All Optional Arguments

Usage

Usage is basically the same as for bootstrap_paginate. The simplest usage is:

    {% bootstrap_pager page_obj %}

A somewhat more advanced usage might look like:

    {% bootstrap_pager page_obj previous_label="Newer Posts" next_label="Older Posts" url_view_name="post_archive_paginated" %}