Search code examples
djangodjango-viewspaginator

How can i determine number object show per page when using ListView pagination in django?


please how can i determine number of objects/page show(not number of pages) in Django when using ListView Pagination. that's my code in Views.py :

from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Post

class PostList(ListView):
    model=Post
    context_object_name='all_post'
    ordering=['-created_at']

thank you!


Solution

  • Just add paginate_by = <number of items in a page> to your view.

    For example:

    from django.shortcuts import render
    from django.views.generic import ListView, DetailView
    from .models import Post
    
    class PostList(ListView):
        model=Post
        context_object_name='all_post'
        ordering=['-created_at']
        paginate_by = 10 # 10 items in a page