From 31e1e0a2eece094ebc16dc1d289d0291ed921cce Mon Sep 17 00:00:00 2001 From: mtyton Date: Sun, 14 May 2023 17:14:44 +0200 Subject: [PATCH] added pagination --- artel/artel/settings/base.py | 3 ++ artel/store/models.py | 42 +++++++++++++------ .../store/partials/product_card.html | 16 ++++--- .../templates/store/product_list_page.html | 33 ++++++++++----- 4 files changed, 65 insertions(+), 29 deletions(-) diff --git a/artel/artel/settings/base.py b/artel/artel/settings/base.py index 36fcf80..fad24bd 100644 --- a/artel/artel/settings/base.py +++ b/artel/artel/settings/base.py @@ -170,3 +170,6 @@ WAGTAILSEARCH_BACKENDS = { # Base URL to use when referring to full URLs within the Wagtail admin backend - # e.g. in notification emails. Don't include '/admin' or a trailing slash WAGTAILADMIN_BASE_URL = "http://example.com" + +# STORE SETTINGS +PRODUCTS_PER_PAGE = 6 diff --git a/artel/store/models.py b/artel/store/models.py index d770de2..2c46b8a 100644 --- a/artel/store/models.py +++ b/artel/store/models.py @@ -1,4 +1,9 @@ from django.db import models +from django.core.paginator import ( + Paginator, + EmptyPage +) +from django.conf import settings from modelcluster.models import ClusterableModel from modelcluster.fields import ParentalKey @@ -6,13 +11,9 @@ from wagtail.admin.panels import ( FieldPanel, InlinePanel ) -from wagtail.models import ( - Orderable, - Page -) +from wagtail.models import Page from wagtail import fields as wagtail_fields from taggit.managers import TaggableManager -from taggit.models import Tag class ProductAuthor(models.Model): @@ -62,17 +63,14 @@ class ProductTemplate(ClusterableModel): def __str__(self): return self.title - def get_images(self): - return self.images.objects.all().values_list("image") - panels = [ FieldPanel("category"), FieldPanel("author"), - FieldPanel("family"), FieldPanel('title'), FieldPanel('code'), FieldPanel('description'), - InlinePanel("images") + InlinePanel("images"), + FieldPanel("tags"), ] @@ -92,9 +90,16 @@ class Product(ClusterableModel): FieldPanel("template"), FieldPanel("price"), InlinePanel("param_values"), - InlinePanel("available") + FieldPanel("available") ] + @property + def main_image(self): + images = self.template.images.all() + print(images) + if images: + return images.first().image + @property def title(self): return f"{self.template.title} - {self.price}" @@ -113,13 +118,24 @@ class ProductListPage(Page): tags = TaggableManager(blank=True) def _get_items(self): - if self.tags: + if self.tags.all(): return Product.objects.filter(available=True, template__tags__in=self.tags.all()) return Product.objects.filter(available=True) def get_context(self, request): context = super().get_context(request) - context["items"] = self._get_items() + items = self._get_items() + if not items: + return context + + paginator = Paginator(items, settings.PRODUCTS_PER_PAGE) + page_number = request.GET.get("page", 1) + try: + page = paginator.page(page_number) + except EmptyPage: + page = paginator.page(1) + context["items"] = page.object_list + context["page"] = page return context content_panels = Page.content_panels + [ diff --git a/artel/store/templates/store/partials/product_card.html b/artel/store/templates/store/partials/product_card.html index f6d13db..ad6acc0 100644 --- a/artel/store/templates/store/partials/product_card.html +++ b/artel/store/templates/store/partials/product_card.html @@ -1,12 +1,18 @@ {% load static %} -
- Card image cap + +
+
{{item.title}}
+ \ No newline at end of file diff --git a/artel/store/templates/store/product_list_page.html b/artel/store/templates/store/product_list_page.html index 9628f27..3238d59 100644 --- a/artel/store/templates/store/product_list_page.html +++ b/artel/store/templates/store/product_list_page.html @@ -3,17 +3,28 @@ {% block content %} -
-
-
- {% include 'store/partials/product_card.html' %} -
-
- {% include 'store/partials/product_card.html' %} -
-
- {% include 'store/partials/product_card.html' %} -
+
+
+ {% for item in items %} +
+ {% include 'store/partials/product_card.html' %} +
+ {% endfor %}
+
+ + {% endblock %}