Issue
I am trying to implement pagination using the Shopify API in a Django App but I am not sure how to do so. I read the documentation and have to know that we can get the link of the next and previous page of a resource endpoint through the API but cannot figure out how to implement it in a Django App.
My view looks like this:
import shopify
from shopify_app.decorators import shop_login_required
@shop_login_required
def index(request):
orders = shopify.Order.find(status='any')
return render(request, 'home/index.html', {'orders': orders})
index.html:
{% extends "base.html" %}
{% load template_tags %}
{% block content %}
<div id="orders">
<div style="display: flex;">
<h2 style="width: 50%;">Your recent orders</h2>
</div>
{% if orders %}
<div class="order-list">
<p id="page-toggle">
<a href="#">
<span id="prev-page"><
<b>PREV PAGE</b>
</span>
</a>
<a href="">
<span id="next-page">
<b>NEXT PAGE</b>>
</span>
</a>
</p>
{% for order in orders %}
<div class="order box">
<div class="wrapper">
<a style='width: 25%; color: black;' href="https://{{ current_shop.domain }}/admin/orders/{{ order.id }}"
target="_blank">{{ order.name }}</a>
<span style='width: 25%;'>{{ order.created_at | convert_str_date }}</span>
<span style='width: 25%;'>{{ order.total_price }} {{ order.currency }}</span>
<span style='width: 25%;'>From: {{ order.billing_address.name }}</span>
</div>
<a href="#" class="download-button">
<button>DOWNLOAD</button>
</a>
</div>
{% endfor %}
</div>
{% else %}
<em class="note">There are no orders in your store.</em>
{% endif %}
</div>
{% endblock %}
Does anyone know how pagination can be implemented?
Solution
I tried the solution mentioned in this question by @rseabrook.
Here's my views.py-
import shopify
from shopify_app.decorators import shop_login_required
from django.core.paginator import Paginator
@shop_login_required
def index(request):
order_list = []
for order in iter_all_orders():
order_list.append(order)
order_list.reverse()
paginator = Paginator(order_list, 50)
page_number = request.GET.get('page')
data = paginator.get_page(page_number)
return render(request, 'home/index.html', {'orders': data})
def iter_all_orders(status='any'):
orders = shopify.Order.find(since_id=0, status=status)
for order in orders:
yield order
while orders.has_next_page():
orders = orders.next_page()
for order in orders:
yield order
And index.html-
{% extends "base.html" %}
{% load template_tags %}
{% block content %}
<div id="orders">
<div style="display: flex;">
<h2 style="width: 50%;">Your recent orders</h2>
</div>
{% if orders %}
<div class="order-list">
<p id="page-toggle">
{% if orders.has_previous %}
<a href="?page={{ orders.previous_page_number }}">
<span id="prev-page"><
<b>PREV PAGE</b>
</span>
</a>
{% endif %}
{% if orders.has_next %}
<a href="?page={{ orders.next_page_number }}">
<span id="next-page">
<b>NEXT PAGE</b>>
</span>
</a>
{% endif %}
</p>
{% for order in orders %}
<div class="order box">
<div class="wrapper">
<a style='width: 25%; color: black;' href="https://{{ current_shop.domain }}/admin/orders/{{ order.id }}"
target="_blank">{{ order.name }}</a>
<span style='width: 25%;'>{{ order.created_at | convert_str_date }}</span>
<span style='width: 25%;'>{{ order.total_price }} {{ order.currency }}</span>
<span style='width: 25%;'>From: {{ order.billing_address.name }}</span>
</div>
<a href="#" class="download-button">
<button>DOWNLOAD</button>
</a>
</div>
{% endfor %}
</div>
{% else %}
<em class="note">There are no orders in your store.</em>
{% endif %}
</div>
{% endblock %}
Answered By - royalsanga
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.