Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add pagination

Adam Bogdałccc3383f708b
diff --git a/saleor/core/templates/bootstrap/_pagination.html b/saleor/core/templates/bootstrap/_pagination.html
new file mode 100644
index 0000000000..fc85179695
--- /dev/null
+++ b/saleor/core/templates/bootstrap/_pagination.html
@@ -0,0 +1,16 @@
+{% load i18n %}
+{% if items.paginator.num_pages > 1 %}
+ <nav>
+ <ul class="pagination">
+ <li {% if not items.has_previous %}class="disabled"{% endif %}><a href="{% if items.has_previous%}?page={{ items.previous_page_number }}{% else %}#{% endif %}" aria-label="{% trans "Previous" %}"><span aria-hidden="true">&laquo;</span></a></li>
+ {% for page in pages %}
+ {% if page == current_page %}
+ <li class="active"><a href="#">{{ page }} <span class="sr-only">(current)</span></a></li>
+ {% else %}
+ <li><a href="?page={{ page }}">{{ page }}</a></li>
+ {% endif %}
+ {% endfor %}
+ <li {% if not items.has_next %}class="disabled"{% endif %}><a href="{% if items.has_next%}?page={{ items.next_page_number }}{% else %}#{% endif %}" aria-label="{% trans "Next" %}"><span aria-hidden="true">&raquo;</span></a></li>
+ </ul>
+ </nav>
+{% endif %}
\ No newline at end of file
diff --git a/saleor/core/templatetags/bootstrap.py b/saleor/core/templatetags/bootstrap.py
index f9d280ad46..ddc8674ba7 100644
--- a/saleor/core/templatetags/bootstrap.py
+++ b/saleor/core/templatetags/bootstrap.py
@@ -10,6 +10,7 @@ register = Library()
TEMPLATE_ERRORS = 'bootstrap/_non_field_errors.html'
TEMPLATE_HORIZONTAL = 'bootstrap/_field_horizontal.html'
TEMPLATE_VERTICAL = 'bootstrap/_field_vertical.html'
+TEMPLATE_PAGINATION = 'bootstrap/_pagination.html'
def render_non_field_errors(errors):
@@ -41,6 +42,13 @@ def render_field(bound_field, show_label, template):
return render_to_string(template, context_instance=context)
+def render_pagination(items, current_page):
+ context = Context({
+ 'items': items, 'current_page': current_page,
+ 'pages': map(str, range(1, items.paginator.num_pages + 1))})
+ return render_to_string(TEMPLATE_PAGINATION, context_instance=context)
+
+
def as_bootstrap(obj, show_label, template):
if isinstance(obj, BoundField):
return render_field(obj, show_label, template)
@@ -71,3 +79,9 @@ def as_vertical_form(obj, show_label=True):
@register.simple_tag
def render_widget(obj, **attrs):
return obj.as_widget(attrs=attrs)
+
+
[email protected]_tag(takes_context=True)
+def pagination(context, items):
+ current_page = context['request'].GET.get('page', '1')
+ return render_pagination(items, current_page)
diff --git a/saleor/core/utils/__init__.py b/saleor/core/utils/__init__.py
index af9b0d84ca..327159e8d3 100644
--- a/saleor/core/utils/__init__.py
+++ b/saleor/core/utils/__init__.py
@@ -3,6 +3,8 @@ from __future__ import unicode_literals
import re
from django import forms
+from django.conf import settings
+from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.template.response import TemplateResponse
from django.utils.encoding import iri_to_uri, smart_text
from satchless.process import InvalidData, Step
@@ -81,3 +83,14 @@ def build_absolute_uri(location, is_secure=False):
current_uri = '%s://%s' % ('https' if is_secure else 'http', host)
location = urljoin(current_uri, location)
return iri_to_uri(location)
+
+
+def get_paginator_items(items, page):
+ paginator = Paginator(items, settings.PAGINATE_BY)
+ try:
+ items = paginator.page(page)
+ except PageNotAnInteger:
+ items = paginator.page(1)
+ except EmptyPage:
+ items = paginator.page(paginator.num_pages)
+ return items
diff --git a/saleor/product/templates/category/index.html b/saleor/product/templates/category/index.html
index 6dae47d9b5..99ce934e98 100644
--- a/saleor/product/templates/category/index.html
+++ b/saleor/product/templates/category/index.html
@@ -1,6 +1,7 @@
{% extends "base.html" %}
{% load slice from shop %}
{% load static from staticfiles %}
+{% load pagination from bootstrap %}
{% block title %}{{ category }} — {{ block.super }}{% endblock %}
@@ -39,6 +40,7 @@
<div class="row">
{% include "category/_items.html" %}
</div>
+ {% pagination products %}
</div>
</div>
{% endblock content %}
diff --git a/saleor/product/views.py b/saleor/product/views.py
index c7b83d613c..d8fa763e37 100644
--- a/saleor/product/views.py
+++ b/saleor/product/views.py
@@ -8,7 +8,8 @@ from django.utils.translation import ugettext as _
from .forms import get_form_class_for_product
from .models import Product, Category
-from saleor.cart import Cart
+from ..cart import Cart
+from ..core.utils import get_paginator_items
def product_details(request, slug, product_id):
@@ -47,6 +48,7 @@ def category_index(request, path, category_id):
category_id=category_id)
products = category.products.get_available_products().select_subclasses()
products = products.prefetch_related('images', 'variants', 'variants__stock')
+ products = get_paginator_items(products, request.GET.get('page'))
return TemplateResponse(
request, 'category/index.html',
{'products': products, 'category': category,
diff --git a/saleor/settings.py b/saleor/settings.py
index 3bb668f5e4..3f79831eb3 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -238,6 +238,8 @@ MESSAGE_TAGS = {
LOW_STOCK_THRESHOLD = 10
+PAGINATE_BY = 16
+
TEST_RUNNER = ''
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', 'localhost').split()