【问题标题】:Django Pagination "Page not found"Django分页“找不到页面”
【发布时间】:2018-01-25 06:10:35
【问题描述】:

我目前的 Django 分页存在问题。我有一个包含 9 个对象的查询集,并且正在将一个对象分页到一个页面。所以我总共应该有 9 页。

Paginator 显示我有 9 页,但是当我点击“下一页”按钮时(总是在最后/最后两页)我的 url 变为:http://127.0.0.1:8000/forum/1?page=7

然后我得到一个 404 page not found 错误以及“无效页面 (7):该页面不包含任何结果”

这是我的课程代码:

class DirectoryClass(generic.ListView):
    template_name = "forum_directory.html"
    model = Directory
    paginate_by = 1

    def get_context_data(self, **kwargs):
        context = super(DirectoryClass, self).get_context_data(**kwargs)
        directory = Directory.objects.filter(pk=self.kwargs['directory_id'])
        context['page'] = 'forum'
        context['name'] = directory.first().name
        context['user'] = self.request.user
        topic_set = directory.first().topic_set.all().order_by('-last_post')
        print(topic_set.all())
        paginator = Paginator(topic_set, self.paginate_by)
        page = self.request.GET.get('page')
        try:
            topic_set = paginator.page(page)
        except PageNotAnInteger:
            topic_set = paginator.page(1)
        except EmptyPage:
            topic_set = paginator.page(paginator.num_pages)
        context['topics'] = topic_set
        context['page'] = page
        return context

这是用于更改页面的 HTML:

<div style="width: 1240px; margin: auto; text-align: right;">
    {% if topics.has_other_pages %}
      <ul class="pagination forum-pagination">
        {% if topics.has_previous %}
          <li><a href="?page={{ topics.previous_page_number }}">&laquo;</a></li>
        {% else %}
          <li class="disabled"><span>&laquo;</span></li>
        {% endif %}
        {% for i in topics.paginator.page_range %}
          {% if topics.number == i %}
            <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
          {% else %}
            <li><a href="?page={{ i }}">{{ i }}</a></li>
          {% endif %}
        {% endfor %}
        {% if topics.has_next %}
          <li><a href="?page={{ topics.next_page_number }}">&raquo;</a></li>
        {% else %}
          <li class="disabled"><span>&raquo;</span></li>
        {% endif %}
      </ul>
    {% endif %}
</div>

这是网址

    path(r'/<int:directory_id>',views.DirectoryClass.as_view(), name='forum_directory'),

我在这里做错了什么?

【问题讨论】:

  • 你也可以发布你的html
  • 添加了html和url!
  • 也许this snippet 会帮助你..
  • 它在页面范围内。我有 7 个项目,它显示了模板上的所有 7 个页面,但是当我滚动浏览时,我只点击了一个。然后,当我点击第 7 页时,出现空对象错误。

标签: python django pagination


【解决方案1】:
def get_context_data(self, **kwargs):
        context = super(DirectoryClass, self).get_context_data(**kwargs)

        context['page'] = 'forum'
        context['user'] = self.request.user

        return context

def get_queryset(self):
    directory = Directory.objects.filter(pk=self.kwargs['directory_id'])
    topics = directory.first().topic_set.all().order_by('-last_post')
    return topics

仅在您的 ListView 中使用它,因为 Django 为所有基于类的视图提供分页

【讨论】:

  • Django 提供的限制是什么?在分页之前,我不想要一个包含数百个结果的页面。
  • 它将根据您在开始时提到的 paginate_by 进行限制,因此 django 将仅分页 1
  • 我刚试过这个,但它不起作用 -.. 它只是用查询集中的所有项目填充页面而不分页
  • @Exorator 我们越来越近了-.. 现在什么都没有显示。所以它没有给我我的查询集。
  • 会不会是因为它无法访问参数?
猜你喜欢
  • 1970-01-01
  • 2020-12-05
  • 2016-08-01
  • 2016-08-01
  • 2015-10-22
  • 2014-03-20
  • 2019-01-22
  • 2020-10-27
  • 2019-07-21
相关资源
最近更新 更多