user19534570
user19534570

Reputation:

Use order by in pagination result Django

I want to order by only pagination result, for example in the first page I get two products (the meaning of this mehsul is a product) and when I get ordering I want to order these two products.

    def animal(request,slug):
        mehsul=Product.objects.filter(animal_type=slug)
        animal_count=2
        paginator=Paginator(mehsul,animal_count)
        page_number=request.GET.get('page')
        page_obj = paginator.get_page(page_number)
        ordering = request.GET.get('ordering')

    if ordering:
        mehsul = Product.objects.filter(animal_type=slug)
        paginator=Paginator(mehsul,animal_count)
        page_number=request.GET.get('page')
        page_obj=paginator.get_page(page_number)
    return render(request,'animal.html',context={'mehsullar':page_obj})

Upvotes: 1

Views: 1638

Answers (1)

0sVoid
0sVoid

Reputation: 2663

Do you just want to order the queryset you've created? If so, you can use the Django ORM order_by() function - see the Django documentation:

https://docs.djangoproject.com/en/4.0/ref/models/querysets/#order-by

Your code that follows if ordering: is a duplicate of the code that precedes it, I would suggest checking the ordering at the start of your view and then putting it into the order_by() function if it exists

Upvotes: 1

Related Questions