Django how to show paginations correctly for each user?

Right now my paginations is same for all user. Let you explain. Assume I listed 2 items per page if user Jhone have 4 items then he will see total 2 page number. see the picture: enter image description here

But User Mick haven’t any items in his account why he is seeing only page numbers? see the picture

enter image description here

here is my code:

 def ShowAuthorNOtifications(request):
        user = request.user
        notifications = filters.NotificationFilter(
        paginator = Paginator(notifications, 5)
        page = request.GET.get('page')
            page_obj =
        except PageNotAnInteger:
            page_obj =
        except EmptyPage:
            page_obj =
        notification_user =  Notifications.objects.filter(user=user).count()
        Notifications.objects.filter(user=user, is_seen=False).update(is_seen=True)
        template_name ='blog/author_notifications.html'
        context = {
            'notifications': notifications,
        return render(request,template_name,context)


{% for notification in page_obj.object_list  %} 
{%if == %}
#my code

#my pagination code:

<!-- Pagination-->       
 <ul class="pagination justify-content-center mb-4">
    {% if page_obj.has_previous %}
    <li class="page-item"><a class="page-link" href="?page=1">First Page</a></li>
    <li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">← Back</a></li>
    {% endif %}

    {% if page_obj.has_next %}
    <li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">Next Page →</a></li>
    {% endif %}

    {% for i in  page_obj.paginator.page_range %}
    {% if page_obj.number == i %}
    <li class="page-item"><a class="page-link" href="#!">{{ i }}</a></li>
    {% elif i > page_obj.number|add:'-3' and i < page_obj.number|add:'3' %}
    <li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
    {% endfor %}
    <li class="page-item"><a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">Last Page</a></li>


If tried to use pagination inside my forloop but it’s showing page number after each items. How to show paginations correctly for each user?

The mistake here is building the paginator based on a queryset that retrieves all rows instead of building it based on the queryset being used to just fetch the rows for that specific user.

1 Like

How to write right quaryset so user can only see their object in pagenation page ???

Review the Retrieving Objects docs.
(It looks like you might already have the query you want in your view(?))

1 Like

Thanks KenWhitesell. My problems solved now. My previous queryset returing all object after applying this custom filter Notifications.objects.all().filter(user=user).order_by('-date') my problem solved