1👍
✅
You are sending my_product_list
to the template, which is your entire queryset before pagination. Your context dictionary should instead look like:
context = {
"form":form,
"products":queryset,
}
And then in the template, use {% products.has_next %}
and so on.
To be honest, I’d prefer different names, such as calling your page simply page
, instead of queryset
, but the above should work. Nicer version, IMO:
form = ProdForm(request.POST or None,request.FILES or None)
product_list = Add_prod.objects.all()
paginator = Paginator(my_product_list,5)
page = request.GET.get('page')
try:
product_list = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
product_list = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
product_list = paginator.page(paginator.num_pages)
context = {
"form": form,
"product_list": product_list,
}
And template:
{% for i in product_list %}
<tr>
<td>{{i.book}}</td>
<td>{{i.author}}</td>
<td>{{i.price}}</td>
<td>{{i.cat}}</td>
<td><img src="{{i.image.url}}" alt="No Image" width=196px height=196px></td>
<td><button><a href="{% url 'update_prod' pk=i.pk %}">Edit</a></button>
<button onclick="return confirm('Are you sure want to delete?');"><a href="{% url 'del_prod' pk=i.pk %}">Delete</a></button></td>
</tr>
{% endfor %}
</table><br>
<div class="pagination">
<span class="step-links">
{% if product_list.has_previous %}
<a href="?page={{ product_list.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ product_list.number }} of {{ product_list.paginator.num_pages }}.
</span>
{% if product_list.has_next %}
<a href="?page={{ product_list.next_page_number }}">next</a>
{% endif %}
</span>
</div>
Source:stackexchange.com