django Posts not Paginating未分页

yxyvkwin  于 2023-07-01  发布在  Go
关注(0)|答案(1)|浏览(69)

我有一个Django应用程序,可以显示使用Django内置管理界面创建的帖子。这些帖子使用django-taggit(https://django-taggit.readthedocs.io/en/latest/)合并了标签
主页(home.html)被设置为显示帖子和标签,当点击标签时,它会将您带到一个页面(tag_posts.html),其中包含所有标记的帖子,例如。如果我点击一个带有“苹果”标签的帖子,我会看到一个页面,显示所有带有“苹果”标签的帖子。主页按预期工作,home.html的分页也是如此。

问题:当查看标记的帖子列表时,它显示了paginate_by指定的帖子数量(在代码中为2),但没有显示单击下一个/上一个或页码的选项。
我所做的一切

  • 我以为这可能是HTML文件中的Bootstrap导航链接,但我使用的是与我的home.html相同的链接,它可以正常工作。
  • 我重构了我的基于类的视图,将标记作为上下文的一部分,并将其作为上下文变量提供给html
  • 使用基本的html作为导航链接

问题出在TagPostsView
以下是我的view.py

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponse
from django.views.generic import ListView, TemplateView
from .models import Session, PostsInSession
from django.core.paginator import Paginator
from taggit.models import Tag


class PostView(ListView):

    queryset = Session.objects.prefetch_related('postsinsession_set').all()
    context_object_name = 'sessions'
    template_name = 'home.html'
    paginate_by = 2  # Number of items per page

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        tags = Tag.objects.order_by('name')
        paginator = Paginator(self.queryset, self.paginate_by)
        page = self.request.GET.get('page')
        sessions = paginator.get_page(page)
        context['sessions'] = sessions
        context['tags'] = tags
        return context



class TagPostsView(ListView):
    template_name = 'tag_posts.html'
    context_object_name = 'posts'
    paginate_by = 2

    def get_queryset(self):
        tag_slug = self.get_tag_slug()
        posts = PostsInSession.objects.filter(post__tags__slug=tag_slug)
        return posts

    def get_tag_slug(self):
        return self.kwargs['tag_slug']

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        tag_slug = self.get_tag_slug()
        tag = get_object_or_404(Tag, slug=tag_slug)
        paginator = Paginator(context['posts'], self.paginate_by)
        page = self.request.GET.get('page')
        paginated_posts = paginator.get_page(page)
        context['tag'] = tag
        context['posts'] = paginated_posts
        return context

tag_posts.html

{% extends "base.html" %}
{% load markdownify %}

{% block content %}
<div class="row">
  <div class="col">
    <br>
    {% if tag %}
      <h2>Tag: {{ tag.name }}</h2>
    {% endif %}
    <hr>
    {% for post in posts %}
    <div class="card m-3 text-center">

      <div class="card-header">
        {{ post.post.title }}
      </div>

      <div class="card-body">
        {{ post.post.body|markdownify }}
      </div>

    </div>
    {% endfor %}
    <nav aria-label="Page navigation">
      <ul class="pagination justify-content-center">
        {% if posts.has_previous %}
          <li class="page-item">
            <a class="page-link" href="?page={{ posts.previous_page_number }}" aria-label="Previous">
              <span aria-hidden="true">&laquo;</span>
            </a>
          </li>
        {% endif %}
        
        {% for num in posts.paginator.page_range %}
          {% if posts.number == num %}
            <li class="page-item active" aria-current="page">
              <span class="page-link">{{ num }}</span>
            </li>
          {% else %}
            <li class="page-item">
              <a class="page-link" href="?page={{ num }}">{{ num }}</a>
            </li>
          {% endif %}
        {% endfor %}
        
        {% if posts.has_next %}
          <li class="page-item">
            <a class="page-link" href="?page={{ posts.next_page_number }}" aria-label="Next">
              <span aria-hidden="true">&raquo;</span>
            </a>
          </li>
        {% endif %}
      </ul>
    </nav>
  </div>
</div>
{% endblock %}
thigvfpy

thigvfpy1#

我终于找到了解决办法。
TagPostsViewget_context_data()中的paginator示例应该接受查询集,而不是context['posts']
这是因为Paginator类接受查询集并拆分为页面对象(请参见:https://docs.djangoproject.com/en/4.2/topics/pagination/)。
我的原始代码是对已经分页的帖子进行分页。下面是固定代码:

def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        tag_slug = self.get_tag_slug()
        tag = get_object_or_404(Tag, slug=tag_slug)
        **paginator = Paginator(self.get_queryset(), self.paginate_by)**
        page = self.request.GET.get('page')
        paginated_posts = paginator.get_page(page)
        context['tag'] = tag
        context['posts'] = paginated_posts
        return context

相关问题