Answer the question
In order to leave comments, you need to log in
How to display database objects using pagination?
Page pagination partially works, i.e. the database does not display data, but
views.py switches between pages
def catalogue(request):
list_of_products = Product.objects.all()
page = request.GET.get('page', 1)
paginator = Paginator(list_of_products, 4)
try:
products = paginator.page(page)
except PageNotAnInteger:
products = paginator.page(1)
except EmptyPage:
products = paginator.page(paginator.num_pages)
return render(request, 'catalogue.html', {'products': products})
{% if products.count > 0 %}
{% for product in products %}
<div class="card w-50 content" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">{{product.Name}}</h5>
<p class="card-text">{{product.Description}}</p>
<a href="#" class="btn btn-primary">${{product.Price}}</a>
</div>
</div>
{% endfor %}
{% endif %}
<nav aria-label=" ">
<ul class="pagination justify-content-center">
{% if products.number <= 3 %}
{% if products.has_previous %}
<li class="page-item"><a class="page-link" href="?page={{ products.previous_page_number }}">Предыдущая</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="">Предыдущая</a></li>
{% endif %}
<li class="page-item"><a class="page-link" href="?page=1">1</a></li>
<li class="page-item"><a class="page-link" href="?page=2">2</a></li>
<li class="page-item"><a class="page-link" href="?page=3">3</a></li>
{% if products.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ products.next_page_number }}">Следующая</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="">Следующая</a></li>
{% endif %}
{% endif %}
</ul>
</nav>
path('catalogue', views.catalogue)
Answer the question
In order to leave comments, you need to log in
Why don't you use a ListView with ready-made logic? This is the first, second, take a product instance and display its attributes using dir() (or look at the models), maybe something is wrong with your attribute names? The third one needs to be replaced with the meaning will not change, the code will become shorter and, most likely, you have a problem here, the paginator is not a querieset. {% if products.count > 0 %}
{% if products %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question