分页 Wagtail 网站最后一页出现 EmptyPage 错误

EmptyPage error on final page of paginated Wagtail site

我搜索了网站,none 的答案与我试过的不同: Example | | |

我的目标是在我的 Wagtail 网站上每个分页页面显示 5 个帖子。它的工作原理除了我在最后一页上得到一个持续的 EmptyPage 错误。我目前有 25 个实时帖子,所以 5 个页面中的每个页面应该恰好有 5 个。只有第 5 页(目前是最后一页)是空的。

我很确定问题出在模板上,而不是 queryset/model 本身,但我在网上看到的一切看起来都与我所拥有的完全相同。

这是我的模型:

class BlogIndexPage(Page):

    body = RichTextField(blank=True)

    def get_context(self, request, *args, **kwargs):
        context = super().get_context(request, *args, **kwargs)
        blog_pages = BlogPage.objects.live().public().order_by("-date")
        page = request.GET.get("page")
        paginator = Paginator(blog_pages, 5)

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


        context["blog_pages"] = blog_pages
        return context

这是我的模板:

{% if blog_pages.paginator.num_pages > 1 %}

   {% if blog_pages.has_previous %}
      <li class="page-item">
        <a href="?page={{ blog_pages.previous_page_number }}" class="page-link">
          <span>&laquo;</span>
        </a>
      </li>
   {% endif %}


   {% for page_num in blog_pages.paginator.page_range %}
      <li class="page-item {% if page_num == blog_pages.number %} active{% endif %}">
         <a href="?page={{ blog_pages.next_page_number }}" class="page-link">
            {{ page_num }}
         </a>
      </li>
   {% endfor %}
    

   {% if blog_pages.has_next %}
     <li class="page-item">
       <a href="?page={{ blog_pages.next_page_number }}" class="page-link">
         <span>&raquo;</span>
       </a>
     </li>
   {% endif %}

{% endif %}  

回溯:

EmptyPage at /blog/
That page contains no results
Request Method: GET
Request URL:    https://***/?page=5
Django Version: 3.1.12
Exception Type: EmptyPage
Exception Value:    
That page contains no results
Exception Location: /***/venv/lib/python3.6/site-packages/django/core/paginator.py, line 55, in validate_number
Python Executable:  /***/venv/bin/python
Python Version: 3.6.9
Python Path:    
['/***/mysite',
 '/***/venv/bin',
 '/usr/lib/python36.zip',
 '/usr/lib/python3.6',
 '/usr/lib/python3.6/lib-dynload',
 '/***/venv/lib/python3.6/site-packages']
Server time:    Wed, 06 Oct 2021 07:34:39 +0200
Error during template rendering
In template /***/mysite/mysite/templates/base.html, error at line 0

That page contains no results
1   {% load static wagtailuserbar %}
2   
3   <!DOCTYPE html>
4   <html class="no-js" lang="en">
5       <head>
6           <meta charset="utf-8" />
7           <title>
8               {% block title %}
9                   {% if self.seo_title %}{{ self.seo_title }}{% else %}{{ self.title }}{% endif %}
10              {% endblock %}
Traceback Switch to copy-and-paste view
/***/venv/lib/python3.6/site-packages/django/core/handlers/exception.py, line 47, in inner
                response = get_response(request) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/core/handlers/base.py, line 204, in _get_response
                response = response.render() …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/response.py, line 105, in render
            self.content = self.rendered_content …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/response.py, line 83, in rendered_content
        return template.render(context, self._request) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/backends/django.py, line 61, in render
            return self.template.render(context) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/base.py, line 170, in render
                    return self._render(context) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/base.py, line 162, in _render
        return self.nodelist.render(context) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/base.py, line 938, in render
                bit = node.render_annotated(context) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/base.py, line 905, in render_annotated
            return self.render(context) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/loader_tags.py, line 150, in render
            return compiled_parent._render(context) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/base.py, line 162, in _render
        return self.nodelist.render(context) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/base.py, line 938, in render
                bit = node.render_annotated(context) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/base.py, line 905, in render_annotated
            return self.render(context) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/loader_tags.py, line 62, in render
                result = block.nodelist.render(context) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/base.py, line 938, in render
                bit = node.render_annotated(context) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/base.py, line 905, in render_annotated
            return self.render(context) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/defaulttags.py, line 312, in render
                return nodelist.render(context) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/base.py, line 938, in render
                bit = node.render_annotated(context) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/base.py, line 905, in render_annotated
            return self.render(context) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/defaulttags.py, line 211, in render
                    nodelist.append(node.render_annotated(context)) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/base.py, line 905, in render_annotated
            return self.render(context) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/base.py, line 988, in render
            output = self.filter_expression.resolve(context) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/base.py, line 671, in resolve
                obj = self.var.resolve(context) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/base.py, line 796, in resolve
            value = self._resolve_lookup(context) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/template/base.py, line 858, in _resolve_lookup
                            current = current() …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/core/paginator.py, line 167, in next_page_number
        return self.paginator.validate_number(self.number + 1) …
▶ Local vars
/***/venv/lib/python3.6/site-packages/django/core/paginator.py, line 55, in validate_number
                raise EmptyPage(_('That page contains no results')) …

在这段代码中:

{% for page_num in blog_pages.paginator.page_range %}
   <li class="page-item {% if page_num == blog_pages.number %} active{% endif %}">
      <a href="?page={{ blog_pages.next_page_number }}" class="page-link">
        {{ page_num }}
      </a>
   </li>
{% endfor %}

?page={{ blog_pages.next_page_number }} 应该是 ?page={{ page_num }}.