Skip to main content

Django pagination utils

Project description

Django Pagination Utils

django-pagination-utils is a library that extends the default paginator and provides utilities for pagination in Django. This library includes mixins and views that facilitate the implementation of these functionalities in your Django projects.

Installation

To install the library, run the following command:

pip install django-pagination-utils

Usage

PaginatorView

This class provides pagination and ordering for a queryset. To use it, you must create a subclass of PaginatorView and define the necessary attributes.

Ejemplo:
from django_pagination_utils.paginator_view import PaginatorView
from .models import MyModel
from .filters import MyFilterSet

class MyView(PaginatorView):
    model = MyModel
    template_name = 'my_template.html'
    filterset_class = MyFilterSet
    allowed_fields_order = ['field1', 'field2', 'field3']

Attributes

model: The Django model that will be used for the queryset. template_name: The name of the template that will be used to render the view. filterset_class: The filters class that will be applied to the queryset. allowed_fields_order: List of fields that can be used to order the queryset. If not provided, all fields will be allowed.

Mixins

QueryOrderMixin
from django_pagination_utils.mixins.query_order_mixin import QueryOrderMixin

class MyOrderView(QueryOrderMixin):
    model = MyModel
    allowed_fields_order = ['field1', 'field2', 'field3']

QueryFilterMixin

The QueryFilterMixin provides functionalities to filter a queryset.

from django_pagination_utils.mixins.query_filter_mixin import QueryFilterMixin

class MyFilterView(QueryFilterMixin):
    model = MyModel
    filterset_class = MyFilterSet

QueryFilterOrderMixin

The QueryFilterOrderMixin provides functionalities to filter and order a queryset.

from django_pagination_utils.mixins.query_filter_order_mixin import QueryFilterOrderMixin

class MyFilterOrderView(QueryFilterOrderMixin):
    model = MyModel
    filterset_class = MyFilterSet
    allowed_fields_order = ['field1', 'field2', 'field3']

Exceptions

The library defines several exceptions that you can handle in your code:

  • InvalidOrderDirectionException: Raised when the ordering direction is invalid.
  • InvalidOrderFieldException: Raised when the ordering field is invalid.
  • InvalidQueryFilterException: Raised when the query filter is invalid.
  • FormFieldNotFoundException: Raised when a form field is not found.

Example

from django_pagination_utils.paginator_view import PaginatorView
from .models import MyModel
from .filters import MyFilterSet

class MyView(PaginatorView):
    model = MyModel
    template_name = 'my_template.html'
    filterset_class = MyFilterSet
    allowed_fields_order = ['field1', 'field2', 'field3']

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['extra_data'] = 'some extra data'
        return context

In your my_template.html template, you can access the paginated and ordered objects:

{% for obj in page_obj %}
    {{ obj }}
{% endfor %}

<div class="pagination">
    <span class="step-links">
        {% if page_obj.has_previous %}
            <a href="?page=1">&laquo; first</a>
            <a href="?page={{ page_obj.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
        </span>

        {% if page_obj.has_next %}
            <a href="?page={{ page_obj.next_page_number }}">next</a>
            <a href="?page={{ page_obj.paginator.num_pages }}">last &raquo;</a>
        {% endif %}
    </span>
</div>

Licencia

This project is licensed under the MIT License - see the LICENSE file for details.

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

django_pagination_utils-0.1.5.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

django_pagination_utils-0.1.5-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file django_pagination_utils-0.1.5.tar.gz.

File metadata

  • Download URL: django_pagination_utils-0.1.5.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.5

File hashes

Hashes for django_pagination_utils-0.1.5.tar.gz
Algorithm Hash digest
SHA256 e6eaab20f1e3e51be4e42d3bcf214a765575df2885c7f2c1aa4837ef91f5c690
MD5 229c79df5e5e33563164528406816437
BLAKE2b-256 284a1ee29dbe3a0dba2042b51bf0adcd61d8ffd250d58d3c254148475d2fb4ad

See more details on using hashes here.

File details

Details for the file django_pagination_utils-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for django_pagination_utils-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 1c8c63043502ce62e4b860d0702c1891a9c5f38c824143b8c7b38358c6309c37
MD5 6fb422ec8dbc6200290ccac2558d69d4
BLAKE2b-256 dffd96d8cb92a45d61898fd8197343f4c7bd253cc68a056bae5f58cdb3360219

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