Django Pagination

33 views
Skip to first unread message

Akshat Zala

unread,
May 30, 2020, 9:50:47 AM5/30/20
to Django users
Hello,

I am finding it difficult to paginate through all the posts

My views.py :

@login_required
def home_view(request):
"""Display all the post of friends and own posts on the dashboard"""
posts = Post.objects.all().order_by('-date_posted')
media: MEDIA_URL
# 'post':Post.objects.filter(Q(author=request.user) | Q(author__from_user=request.user) | Q(author__to_user=request.user)).order_by('-date_posted'),
paginator = Paginator(posts, 2)
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
return render(request, 'post/home.html',{'page_obj': page_obj})


and in template post/home.html:

{% if is_paginated %}
{% if page_obj.has_previous %}
<a class="btn btn-outline-info mb-4" href="?page=1">First</a>
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.previous_page_number }}"><span class="material-icons">arrow_left</span></a>
{% endif %}
{% for num in page_obj.paginator.page_range %}
{% if page_obj.number == num %}
<a class="btn btn-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% elif num > page_obj.number|add:'-4' and num < page_obj.number|add:'4' %}
<a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.next_page_number }}"><span class="material-icons">arrow_right</span></a>
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
{% endif %}


Thanks

Akshat Zala

Akshat Zala

unread,
May 30, 2020, 9:54:18 AM5/30/20
to Django users

maninder singh Kumar

unread,
May 30, 2020, 11:24:35 AM5/30/20
to django...@googlegroups.com
Views.py looks fine !

regards
willy


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/87c3f0db-3088-448c-b3c7-14450e8c2f5d%40googlegroups.com.

Chetan Ganji

unread,
May 30, 2020, 12:09:52 PM5/30/20
to django...@googlegroups.com
Hi akshat,
What is the difficulty?
Where have you defined the variable   is_paginated? Is it present in the context?

Akshat Zala

unread,
May 31, 2020, 2:23:31 AM5/31/20
to Django users
Hi,

if I change the template to:

{% if not is_paginated %}
{% if page_obj.has_previous %}
<a class="btn btn-outline-info mb-4" href="?page=1">First</a>
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.previous_page_number }}"><span class="material-icons">arrow_left</span></a>
{% endif %}
{% for num in page_obj.paginator.page_range %}
{% if page_obj.number == num %}
<a class="btn btn-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% elif num > page_obj.number|add:'-4' and num < page_obj.number|add:'4' %}
<a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.next_page_number }}"><span class="material-icons">arrow_right</span></a>
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
{% endif %}
{% endif %}

It only shows pagination and posts are not seen in template.

On Saturday, 30 May 2020 21:39:52 UTC+5:30, RyuCoder wrote:
Hi akshat,
What is the difficulty?
Where have you defined the variable   is_paginated? Is it present in the context?

To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Akshat Zala

unread,
Jun 1, 2020, 10:16:24 AM6/1/20
to Django users
Dear Sir,

it shows all the text on all the pages.

if there are total 5 posts, then it  shows all the 5 posts on all the pages.

Thanks,

Akshat Zala
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

maninder singh Kumar

unread,
Jun 1, 2020, 12:56:15 PM6/1/20
to django...@googlegroups.com
I think there might be a way around, below is the code modified for views.py

@login_required
def home_view(request):
"""Display all the post of friends and own posts on the dashboard"""
posts = Post.objects.order_by('-date_posted')
media: MEDIA_URL
# 'post':Post.objects.filter(Q(author=request.user) | Q(author__from_user=request.user) | Q(author__to_user=request.user)).order_by('-date_posted'),
paginator = Paginator(posts, 2)
page_number = request.GET.get('page')
try:
page_obj = paginator.page('page')
except PageNotAnInteger:
page_obj = paginator.page(1)
except EmptyPage:
page_obj = paginator.page(paginator.num_pages)
return render(request, 'post/home.html',{'page_obj': page_obj})

I think this might work !

regards
willy


To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c775d687-0773-4d09-86be-35e6f2cd55d2%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages