From 2f17d01780937793f951af3faa68bd740f9eabb3 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 2 Oct 2024 12:51:40 +0200 Subject: [PATCH] fix pagination --- dashboard/mixins.py | 20 +++++---- dashboard/templates/pagination.html | 47 +++++++++++++++++++++ dashboard/templates/unassigned_devices.html | 4 +- dashboard/templatetags/paginacion.py | 3 +- lot/templates/list_lots.html | 2 +- 5 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 dashboard/templates/pagination.html diff --git a/dashboard/mixins.py b/dashboard/mixins.py index 3ebac58..07004ee 100644 --- a/dashboard/mixins.py +++ b/dashboard/mixins.py @@ -84,14 +84,18 @@ class InventaryMixin(DashboardView, TemplateView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - limit = self.request.GET.get("limit", 10) - page = self.request.GET.get("page", 1) - if limit: - try: - limit = int(limit) - page = int(page) - except: - raise Http404 + limit = self.request.GET.get("limit") + page = self.request.GET.get("page") + try: + limit = int(limit) + page = int(page) + if page < 1: + page = 1 + if limit < 1: + limit = 10 + except: + limit = 10 + page = 1 offset = (page - 1) * limit devices, count = self.get_devices(self.request.user, offset, limit) diff --git a/dashboard/templates/pagination.html b/dashboard/templates/pagination.html new file mode 100644 index 0000000..7a20679 --- /dev/null +++ b/dashboard/templates/pagination.html @@ -0,0 +1,47 @@ +{% load i18n %} +{% load range %} + + diff --git a/dashboard/templates/unassigned_devices.html b/dashboard/templates/unassigned_devices.html index cb8a42c..6df36a7 100644 --- a/dashboard/templates/unassigned_devices.html +++ b/dashboard/templates/unassigned_devices.html @@ -54,12 +54,12 @@ {% endfor %} - +
- {% render_pagination page total_pages %} + {% render_pagination page total_pages limit %}
{% endblock %} diff --git a/dashboard/templatetags/paginacion.py b/dashboard/templatetags/paginacion.py index 7a50113..94ff5fc 100644 --- a/dashboard/templatetags/paginacion.py +++ b/dashboard/templatetags/paginacion.py @@ -3,7 +3,7 @@ from django import template register = template.Library() @register.inclusion_tag('pagination.html') -def render_pagination(page_number, total_pages): +def render_pagination(page_number, total_pages, limit=10): """ Template tag for render pagination @@ -16,4 +16,5 @@ def render_pagination(page_number, total_pages): return { 'page_number': page_number, 'total_pages': total_pages, + 'limit': limit } diff --git a/lot/templates/list_lots.html b/lot/templates/list_lots.html index d11fb3c..4024387 100644 --- a/lot/templates/list_lots.html +++ b/lot/templates/list_lots.html @@ -26,7 +26,7 @@ {% endfor %} {% endfor %} - + {% endblock %}