Paginating Category wise (catalogue) item list

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

 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

 {% for category in page.object_list %}

       {{category}}

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

           {{product.title}}

      {% endfor %}

{% endfor %}

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.

really, any pointer? sir.

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)

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

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 to help you get started here.)