I’m trying to use the paginator inbuild Django module so that the user can pass the pages. The problem is that I’ve configured everything as it should, but the pages numbers are not displaying. The place where it should have the numbers is entirely blank. Why can that due to?
Home Shop Shop<section class="ftco-section bg-light"> <div class="container"> <div class="row"> <div class="col-md-8 col-lg-10 order-md-last"> {% for item in items %} <div class="row"> <div class="col-sm-12 col-md-12 col-lg-4 ftco-animate d-flex"> <div class="product d-flex flex-column"> <a href="{{ item.get_absolute_url }}" class="img-prod"><img class="img-fluid" src='{% static "images/product-1.png" %}' alt="Colorlib Template"> <div class="overlay"></div> </a> <div class="text py-3 pb-4 px-3"> <div class="d-flex"> <div class="cat"> <span>{{ item.get_category_display }}</span> </div> <div class="rating"> <p class="text-right mb-0"> <a href="#"><span class="ion-ios-star-outline"></span></a> <a href="#"><span class="ion-ios-star-outline"></span></a> <a href="#"><span class="ion-ios-star-outline"></span></a> <a href="#"><span class="ion-ios-star-outline"></span></a> <a href="#"><span class="ion-ios-star-outline"></span></a> </p> </div> </div> <h3><a href="{{ item.get_absolute_url }}">{{ item.title }}</a></h3> <div class="pricing"> {% if item.discount_price %} <p class="price"><span>${{ item.discount_price }}</span></p> {% else %} <p class="price"><span>${{ item.price }}</span></p> {% endif %} </div> <p class="bottom-area d-flex px-3"> <a href="{{ item.get_add_to_cart }}" class="add-to-cart text-center py-2 mr-1"><span>Add to cart <i class="ion-ios-add ml-1"></i></span></a> <a href="#" class="buy-now text-center py-2">Buy now<span><i class="ion-ios-cart ml-1"></i></span></a> </p> </div> </div> </div> {% endfor %} </div> <div class="row mt-5"> <div class="col text-center"> <div class="block-27"> {% if items.paginator.num_pages > 1 %} <ul> {% if items.has_previous %} <li><a href="?page={{ page_obj.next_page_number }}"><</a></li> {% endif %} <li class="active"><span>1</span></li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> {% if items.has_next %} <li><a href="#">></a></li> {% endif %} </ul> {% endif %} </div> </div> </div> </div>
views.py:
def shop(request): items = Item.objects.all() paginator = Paginator(items, 5) page = request.GET.get('page') posts = paginator.get_page(page) context = { 'items': Item.objects.all(), 'page': page } return render(request, 'ecommerceapp/shop.html', context)
Any help i would really appreciate it. I’ve been trying to fix this issue for a while.
Advertisement
Answer
You have to use your posts
object.
view.py
def shop(request): items = Item.objects.all() paginator = Paginator(items, 5) page = request.GET.get('page') posts = paginator.get_page(page) context = { 'posts': posts } return render(request, 'ecommerceapp/shop.html', context)
template.html
<section class="ftco-section bg-light"> <div class="container"> <div class="row"> <div class="col-md-8 col-lg-10 order-md-last"> {% for item in posts %} <div class="row"> <div class="col-sm-12 col-md-12 col-lg-4 ftco-animate d-flex"> <div class="product d-flex flex-column"> <a href="{{ item.get_absolute_url }}" class="img-prod"><img class="img-fluid" src='{% static "images/product-1.png" %}' alt="Colorlib Template"> <div class="overlay"></div> </a> <div class="text py-3 pb-4 px-3"> <div class="d-flex"> <div class="cat"> <span>{{ item.get_category_display }}</span> </div> <div class="rating"> <p class="text-right mb-0"> <a href="#"><span class="ion-ios-star-outline"></span></a> <a href="#"><span class="ion-ios-star-outline"></span></a> <a href="#"><span class="ion-ios-star-outline"></span></a> <a href="#"><span class="ion-ios-star-outline"></span></a> <a href="#"><span class="ion-ios-star-outline"></span></a> </p> </div> </div> <h3><a href="{{ item.get_absolute_url }}">{{ item.title }}</a></h3> <div class="pricing"> {% if item.discount_price %} <p class="price"><span>${{ item.discount_price }}</span></p> {% else %} <p class="price"><span>${{ item.price }}</span></p> {% endif %} </div> <p class="bottom-area d-flex px-3"> <a href="{{ item.get_add_to_cart }}" class="add-to-cart text-center py-2 mr-1"><span>Add to cart <i class="ion-ios-add ml-1"></i></span></a> <a href="#" class="buy-now text-center py-2">Buy now<span><i class="ion-ios-cart ml-1"></i></span></a> </p> </div> </div> </div> {% endfor %} </div> <div class="row mt-5"> <div class="col text-center"> <div class="block-27"> {% if posts.paginator.num_pages > 1 %} <ul> {% if posts.has_previous %} <li><a href="?page={{ posts.previous_page_number}}"><</a></li> {% endif %} {% for i in posts.paginator.page_range %} <li><a href="?page={{ i }}">{{ i }}</a></li> {% endfor %} {% if posts.has_next %} <li><a href="?page={{ posts.next_page_number }}">></a></li> {% endif %} </ul> {% endif %} </div> </div> </div> </div>