python 分页器没有限制需要在页面Django上显示的帖子数量?

aiazj4mn  于 2023-01-16  发布在  Python
关注(0)|答案(1)|浏览(129)

我正在写我的Django博客,当我进入分类时,我列出了该分类中的所有帖子,但由于某种原因,我无法管理分页。除了一件事,所有页面上我都可以看到所有帖子,但我希望每页只看到6篇帖子。
这是包含在类别详细信息页面中的pagination.html

<div class="mb-30">
    <nav aria-label="Page navigation example">
        <ul class="pagination justify-content-start">
          {% if category_page.has_previous %}
            <li class="page-item"><a class="page-link" href="?page={{ category_page.previous_page_number }}"><i class="ti-angle-left"></i></a></li>
          {% else %}
            <li class="page-item"><a class="page-link" href="#"><i class="ti-angle-left"></i></a></li>
          {% endif %}
  
          {% for i in category_page.paginator.page_range %}
            {% if category_page.number == i %}
              <li class="page-item active"><a class="page-link" href="#">{{ i }}</a></li>
            {% else %}
              <li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
            {% endif %}
          {% endfor %}
  
          {% if category_page.has_next %}
            <li class="page-item"><a class="page-link" href="?page={{ category_page.next_page_number }}"><i class="ti-angle-right"></i></a></li>
          {% else %}
            <li class="page-item"><a class="page-link" href="#"><i class="ti-angle-right"></i></a></li>
          {% endif %}
        </ul>
    </nav>
</div>

这是分类_详细信息. html

{% for post in posts %}
    <article class="col-lg-10">
        <div class="background-white">
            <div class="post-thumb">
                <a href="{{ post.get_absolute_url }}">
                    <img class="border-radius" src="{{ post.image.standard.url }}" alt="">
                </a>
            </div>
            <div class="pl-10">
                <div class="mb-15">
                    <a class="meta-2" href="{{ post.category.get_absolute_url }}"><span class="post-in">{{ post.category}}</span></a>
                </div>
                <h5 class="mb-15">
                    <a href="{{ post.get_absolute_url }}">{{ post.post_title }}</a></h5>
                <p class="mb-30">{{ post.body | slice:":200" | safe }}</p>
                <div class="mb-10">
                    <span class="post-on">{{ post.created_at}}</span>
                </div>
            </div>
        </div>
    </article>
{% endfor %}
{% include "include/pagination.html" %}

这里是www.example.comviews.py

def category_detail(request, slug):
    category = get_object_or_404(Category, slug=slug)
    categories = Category.objects.all()
    posts = Post.objects.filter(category=category)
    paginator = Paginator(posts, 6)
    page = request.GET.get('page')

    try:
        category_page = paginator.get_page(page)
    except PageNotAnInteger:
        category_page = paginator.get_page(1)
    except EmptyPage:
        category_page = paginator.get_page(paginator.num_pages)

    context = {'category': category, 'categories': categories, 'category_page': category_page, 'posts':posts}
    return render(request, 'category_detail.html', context)

你知道为什么我看到所有的帖子,而不是只有6个吗?
先谢了!

ngynwnxp

ngynwnxp1#

更改您的类别_detail. html
文件:https://docs.djangoproject.com/en/4.1/topics/pagination/

{% for post in category_page %}
    <article class="col-lg-10">
        <div class="background-white">
            <div class="post-thumb">
                <a href="{{ post.get_absolute_url }}">
                    <img class="border-radius" src="{{ post.image.standard.url }}" alt="">
                </a>
            </div>
            <div class="pl-10">
                <div class="mb-15">
                    <a class="meta-2" href="{{ post.category.get_absolute_url }}"><span class="post-in">{{ post.category}}</span></a>
                </div>
                <h5 class="mb-15">
                    <a href="{{ post.get_absolute_url }}">{{ post.post_title }}</a></h5>
                <p class="mb-30">{{ post.body | slice:":200" | safe }}</p>
                <div class="mb-10">
                    <span class="post-on">{{ post.created_at}}</span>
                </div>
            </div>
        </div>
    </article>
{% endfor %}
{% include "include/pagination.html" %}

相关问题