我使用分页器对我的文章进行分页,没有错误,在文章底部有页面列表,但是:
1.Post没有正确分页,我已经设置了5我有更多。2.当我点击第二页和第三页,等等,我有相同的结果,我的文章,我没有下一页与下一篇文章。
这是我的视图代码:
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 -->谢谢你。
发布于 2022-02-22 12:59:23
您可以尝试使用基于类的视图。
你的会是这样的:
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 %}https://stackoverflow.com/questions/71219487
复制相似问题