Skip to content

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

Add pagination as a template tag

michale130129f9ffa
diff --git a/saleor/dashboard/templatetags/utils.py b/saleor/dashboard/templatetags/utils.py
index f54c5166d7..c7a6d33d5e 100644
--- a/saleor/dashboard/templatetags/utils.py
+++ b/saleor/dashboard/templatetags/utils.py
@@ -31,3 +31,14 @@ def is_versatile_image_ppoi_click_widget(field):
def render_image_choice(field):
choices = zip(field, field.field.queryset)
return {'field': field, 'choices_with_images': choices}
+
+
[email protected]_tag('dashboard/includes/_pagination.html',
+ takes_context=True)
+def paginate(context, page_obj, num_of_pages=3):
+ context['page_obj'] = page_obj
+ context['n_forward'] = num_of_pages+1
+ context['n_backward'] = -num_of_pages-1
+ context['next_section'] = (2*num_of_pages)+1
+ context['previous_section'] = (-2*num_of_pages)-1
+ return context
diff --git a/templates/dashboard/includes/_pagination.html b/templates/dashboard/includes/_pagination.html
index 0a86ab16a3..95c29d75a4 100644
--- a/templates/dashboard/includes/_pagination.html
+++ b/templates/dashboard/includes/_pagination.html
@@ -11,8 +11,8 @@
<a href="?page=1">
<svg data-src="{% static "dashboard/images/chevron_left.svg" %}" fill="#000" />
</a>
- {% if page_obj.number|add:'-4' > 1 %}
- <a href="{% construct_get_query page=page_obj.number|add:'-4' %}">
+ {% if page_obj.number|add:previous_section >= 1 %}
+ <a href="{% construct_get_query page=page_obj.number|add:previous_section %}">
&hellip;
</a>
{% endif %}
@@ -29,7 +29,7 @@
{{ page }}
</a>
</li>
- {% elif page > page_obj.number|add:'-2' and page < page_obj.number|add:'2' %}
+ {% elif page > page_obj.number|add:n_backward and page < page_obj.number|add:n_forward %}
<li class="waves-effect">
<a href="{% construct_get_query page=page %}">
{{ page }}
@@ -37,10 +37,10 @@
</li>
{% endif %}
{% endfor %}
- <li{% if not page_obj.has_next %} class="disabled"{% endif %}>
+ <li {% if not page_obj.has_next %} class="disabled"{% endif %} >
{% if page_obj.has_next %}
- {% if page_obj.number|add:'4' < page_obj.paginator.num_pages %}
- <a href="{% construct_get_query page=page_obj.number|add:'4' %}">
+ {% if page_obj.number|add:next_section <= page_obj.paginator.num_pages %}
+ <a href="{% construct_get_query page=page_obj.number|add:next_section %}">
&hellip;
</a>
{% endif %}
diff --git a/templates/dashboard/product/list.html b/templates/dashboard/product/list.html
index 31ebc2d77e..dd711f9870 100644
--- a/templates/dashboard/product/list.html
+++ b/templates/dashboard/product/list.html
@@ -4,6 +4,7 @@
{% load materializecss %}
{% load staticfiles %}
{% load product_first_image from product_images %}
+{% load utils %}
{% block title %}
{% trans "Products" context "Dashboard products list" %} - {{ block.super }}
@@ -62,7 +63,7 @@
</li>
{% endfor %}
</ul>
- {% include "dashboard/includes/_pagination.html" with page_obj=products %}
+ {% paginate products %}
{% else %}
<div class="not-found">
<p class="grey-text">