# Paginating Category wise (catalogue) item list

**URL:** <https://forum.djangoproject.com/t/paginating-category-wise-catalogue-item-list/14869>\
**Category:** Using the ORM\
**Created:** [July 18, 2022, 12:36pm UTC](https://forum.djangoproject.com/t/paginating-category-wise-catalogue-item-list/14869 "2022-07-18T12:36:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kalistos1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kalistos1/32/1843_2.png) [@kalistos1](https://forum.djangoproject.com/u/kalistos1)\
**Post date:** [July 18, 2022, 12:36pm UTC](https://forum.djangoproject.com/t/paginating-category-wise-catalogue-item-list/14869/1 "2022-07-18T12:36:28Z")

</div>

i have a category and a list of items which i was able to display according to the categories, in the following format.

CATEGORY 1  
item1  
item2  
item3 etc

CATEGORY 1  
item1  
item2  
item3 etc

i will want to do a pagination of the items in such a way that for each page i the category and only two (2) items is shown on a page then moves to the next one until the items in the category finishes the the next category follows suit

## page 1

category 1  
item1  
item2

## page 2

item3  
item4

## page 3

category 2  
item1  
item2

## page 4

item3  
item4

I already have the following

```auto
 def books(request):
    categories = Category.objects.all()
    page_number = request.GET.get('page')
    product_paginator = Paginator(categories,1)
    page = product_paginator.get_page(page_number)
  
    context = {
        'page': page,
        'categories': categories,
         }
    return render(request, 'core/index.html', context)

```

in my template i have something

```auto
 {% for category in page.object_list %}

       {{category}}

       {% for product in category.cat_product.all %}

           {{product.title}}

      {% endfor %}

{% endfor %}

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 18, 2022, 12:47pm UTC](https://forum.djangoproject.com/t/paginating-category-wise-catalogue-item-list/14869/2 "2022-07-18T12:47:47Z")

</div>

You’re probably going to need to manage this yourself.

You might be able to take advantage of the Paginator object to help with the rendering of the widgets on the page, but you’re going to need to calculate how a page number translates to a Category and Item set.

---

<div class="post-metadata">

**Author:** ![kalistos1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kalistos1/32/1843_2.png) [@kalistos1](https://forum.djangoproject.com/u/kalistos1)\
**Post date:** [July 18, 2022, 1:03pm UTC](https://forum.djangoproject.com/t/paginating-category-wise-catalogue-item-list/14869/3 "2022-07-18T13:03:08Z")

</div>

really, any pointer? sir.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 18, 2022, 1:09pm UTC](https://forum.djangoproject.com/t/paginating-category-wise-catalogue-item-list/14869/4 "2022-07-18T13:09:36Z")

</div>

You’re going to need to count how many are in each category to figure out which category the page will start on.

For example, if you’ve got 10 items in Category 1, 6 items in Category 2, 5 items in Category 3 and 20 items in Category 4, and if you’re showing page 10, you would be in Category 3, items 3 and 4.

(Pages 1-5 is Category 1, Pages 6-8 is Category 2, Page 9 is Category 3 items 1 & 2, so Page 10 is Category 3 items 3 and 4)

---

<div class="post-metadata">

**Author:** ![kalistos1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kalistos1/32/1843_2.png) [@kalistos1](https://forum.djangoproject.com/u/kalistos1)\
**Post date:** [July 21, 2022, 9:13pm UTC](https://forum.djangoproject.com/t/paginating-category-wise-catalogue-item-list/14869/5 "2022-07-21T21:13:36Z")

</div>

whew still trying to wrap my head around how to do this

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 21, 2022, 9:25pm UTC](https://forum.djangoproject.com/t/paginating-category-wise-catalogue-item-list/14869/6 "2022-07-21T21:25:01Z")

</div>

So start with the first step.

You’ve got a model named Category. You’ve got a model named Item with an FK to Category.

What query could you write to calculate the number of Item related to each Category? (See [Aggregation | Django documentation | Django](https://docs.djangoproject.com/en/4.0/topics/db/aggregation/) to help you get started here.)
