分页器不会分页
Paginator won't paginate
我使用 Paginator 对我的帖子进行分页,我没有错误,并且我在帖子底部有页面列表,但是:
1.Post 没有正确分页,我设置了 5 我还有更多。
2. 当我点击第 2 页和第 3 页等时,我有相同的帖子结果,我没有下一页有下一篇帖子。
这是我的查看代码:
def post_all(request):
posts = Post.objects.filter().order_by('-published_date')
paginator = Paginator(posts, 5)
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
categories = PostCategory.objects.all().annotate(posts_count=Count('post'))
return render(request, 'front/blog/post_all.html', {"posts":posts, "categories":categories,"page_obj":page_obj})
我的模板:
<!-- Pagination -->
<div class="pagination">
{% if page_obj.has_previous %}
<a href=""><i class="fa fa-angle-left"></i></a>
{% endif %}
<a href="" class="active">{{ page_obj.number }}</a> de <a href="" class="active">{{ page_obj.paginator.num_pages }}</a>
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}"><i class="fa fa-angle-right"></i></a>
{% endif %}
</div>
<!-- End Pagination -->
谢谢你
您可以尝试使用 Class-based views。
你的应该是这样的:
views.py
from django.views.generic import (ListView)
class PostAllView(ListView):
model = Post
template_name = 'front/blog/post_all.html' # <app>/<model>_<viewtype>.html
context_object_name = 'posts'
ordering = ['-published_date']
paginate_by = 5
和一个您可以适应自己的template.html
:
<h1>Total of posts : {{ page_obj.paginator.count }}</h1>
{% for post in posts %}
<article>
<p class="article-content">{{ post.content }}</p>
<!-- all the post related content you want to display here-->
</article>
{% endfor %}
{% if is_paginated %}
{% if page_obj.has_previous %}
<!-- you can adapt the class to your use-->
<a class="button" href="?page=1">First</a>
<a class="button" href="?page={{ page_obj.previous_page_number }}">Previous</a>
{% endif %}
{% for num in page_obj.paginator.page_range %}
{% if page_obj.number == num %}
<a class="button-strong" href="?page={{ num }}">{{ num }}</a>
{% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
<a class="button" href="?page={{ num }}">{{ num }}</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a class="button" href="?page={{ page_obj.next_page_number }}">Next</a>
<a class="button" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
{% endif %}
{% endif %}
我使用 Paginator 对我的帖子进行分页,我没有错误,并且我在帖子底部有页面列表,但是:
1.Post 没有正确分页,我设置了 5 我还有更多。 2. 当我点击第 2 页和第 3 页等时,我有相同的帖子结果,我没有下一页有下一篇帖子。
这是我的查看代码:
def post_all(request):
posts = Post.objects.filter().order_by('-published_date')
paginator = Paginator(posts, 5)
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
categories = PostCategory.objects.all().annotate(posts_count=Count('post'))
return render(request, 'front/blog/post_all.html', {"posts":posts, "categories":categories,"page_obj":page_obj})
我的模板:
<!-- Pagination -->
<div class="pagination">
{% if page_obj.has_previous %}
<a href=""><i class="fa fa-angle-left"></i></a>
{% endif %}
<a href="" class="active">{{ page_obj.number }}</a> de <a href="" class="active">{{ page_obj.paginator.num_pages }}</a>
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}"><i class="fa fa-angle-right"></i></a>
{% endif %}
</div>
<!-- End Pagination -->
谢谢你
您可以尝试使用 Class-based views。
你的应该是这样的:
views.py
from django.views.generic import (ListView)
class PostAllView(ListView):
model = Post
template_name = 'front/blog/post_all.html' # <app>/<model>_<viewtype>.html
context_object_name = 'posts'
ordering = ['-published_date']
paginate_by = 5
和一个您可以适应自己的template.html
:
<h1>Total of posts : {{ page_obj.paginator.count }}</h1>
{% for post in posts %}
<article>
<p class="article-content">{{ post.content }}</p>
<!-- all the post related content you want to display here-->
</article>
{% endfor %}
{% if is_paginated %}
{% if page_obj.has_previous %}
<!-- you can adapt the class to your use-->
<a class="button" href="?page=1">First</a>
<a class="button" href="?page={{ page_obj.previous_page_number }}">Previous</a>
{% endif %}
{% for num in page_obj.paginator.page_range %}
{% if page_obj.number == num %}
<a class="button-strong" href="?page={{ num }}">{{ num }}</a>
{% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
<a class="button" href="?page={{ num }}">{{ num }}</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a class="button" href="?page={{ page_obj.next_page_number }}">Next</a>
<a class="button" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
{% endif %}
{% endif %}