Django Pagination

已查看 33 次
跳至第一个未读帖子

Akshat Zala

未读,
2020年5月30日 09:50:472020/5/30
收件人 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

未读,
2020年5月30日 09:54:182020/5/30
收件人 Django users

maninder singh Kumar

未读,
2020年5月30日 11:24:352020/5/30
收件人 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

未读,
2020年5月30日 12:09:522020/5/30
收件人 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

未读,
2020年5月31日 02:23:312020/5/31
收件人 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

未读,
2020年6月1日 10:16:242020/6/1
收件人 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

未读,
2020年6月1日 12:56:152020/6/1
收件人 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.
回复全部
回复作者
转发
0 个新帖子