Django分页“找不到页面" [英] Django Pagination "Page not found"

查看:59
本文介绍了Django分页“找不到页面"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在Django分页方面遇到问题.我有9个对象的查询集,并将一个对象分页到页面.所以我总共应该有9页.

I'm currently having issues with my Django pagination. I have a query set of 9 objects and am paginating one object to a page. So I should have 9 pages total.

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

Paginator is showing that I have 9 pages, but when I click the "next" page button my (Always on the last/last two pages) my url turns to: http://127.0.0.1:8000/forum/1?page=7

然后我得到一个404页面未找到错误以及无效页面(7):该页面没有结果"

Then I get a 404 page not found error along with "Invalid page (7): That page contains no results"

这是我的班级代码:

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:

Here is the HTML used to change the page:

<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'),

我在做什么错了?

推荐答案

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为所有基于类的视图提供了分页功能

use this only in your ListView, as Django provides pagination to all the Class Based View

这篇关于Django分页“找不到页面"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆