Pengin-Open-Source / pengin-pi-2

Pengin-Pi Django version
GNU General Public License v3.0
0 stars 1 forks source link

[FEATURE] Pagination macro #21

Open franfif opened 6 days ago

franfif commented 6 days ago

Explanation Provide consistent pagination layout throughout the application with a template to include in any template using a pagination.

Brief Pagination template tag will take a Django pagination variable and display the number of pages, the previous and next buttons, as well as link to other pages.

franfif commented 6 days ago

Added pagination template to include on any page requiring pagination. The view should use the Django provided Paginator class and return the current page.

I also replaced the call to paginate macro in forums/threads_html and jobs_applications. Other templates have a pagination bu didn't use the macro.

Next: all pages using pagination should include the template "components/pagination.html" passing the current page from the Django Paginator get_page() method.

For instance, in the Product list view:

from django.core.paginator import Paginator

[...]
class ListProduct(View):
    def get(self, request, is_admin):
        # some code to define queryset

        paginator = Paginator(products, 9)  # queryset and number of objects per page
        page_number = request.GET.get("page", 1)
        page_products = paginator.get_page(page_number)

        return render(request, self.template_name, {
                    "is_admin": is_admin,
                    "page_products": page_products,
                    "primary_title": "Products",
                })

and in the product list template:

{% include "components/pagination.html" with page=page_products %}
franfif commented 6 days ago

Switch back to macros using django-macros package. Still using the Django's Paginator class as previously described.

To use the macros in templates:

{% load macros %}  # At the top of the template
{% loadmacros "macros/pagination.html" %}

[...]

{% use_macro render_pagination applications.items %}  # When the macro must be used

I corrected the three places where this macros is being used: job_applications.html, forums/threads.html and products.html.