C
C
cromvwell2020-01-31 18:13:00
Django
cromvwell, 2020-01-31 18:13:00

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})


catalogue.html
{% 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>


Added to urlpatterns Please tell me what's wrong, advise how to do it better if possible
path('catalogue', views.catalogue)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mastermind-S, 2020-02-06
@Mastermind-S

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 question

Ask a Question

731 491 924 answers to any question