Wag的分页

kbd*_*dev 12 django blogs pagination wagtail

我对Wagtail很新,而且我正在创建一个有资源(博客)部分的网站,我不知道如何实现分页,这样每个页面和用户只有5个帖子必须单击一个数字(1,2,3等)才能转到下一页以查看接下来的5个帖子.

我在我的模板中有资源/博客索引页面的分页部分:

<ul class="pagination">
  <li><a href="#"><i class="fa fa-angle-left"></i></a></li>
  <li class="active"><a href="#">1</a></li>
  <li><a href="#">2</a></li>
  <li><a href="#">3</a></li>
  <li><a href="#"><i class="fa fa-angle-right"></i></a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)

我需要使用哪些代码才能实现此功能?提前致谢.

gas*_*man 19

Django django.core.paginator为此提供了模块:https://docs.djangoproject.com/en/1.10/topics/pagination/.在Wagtail中使用它与Django文档中的示例非常相似 - 唯一真正的区别在于,当您设置Paginator要传递给模板的对象时,您可以使用get_context页面模型上的方法,而不是查看功能.您的模型定义将如下所示:

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

class ResourceIndexPage(Page):
    # ...
    def get_context(self, request):
        context = super(ResourceIndexPage, self).get_context(request)

        # Get the full unpaginated listing of resource pages as a queryset -
        # replace this with your own query as appropriate
        all_resources = ResourcePage.objects.live()

        paginator = Paginator(all_resources, 5) # Show 5 resources per page

        page = request.GET.get('page')
        try:
            resources = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            resources = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of results.
            resources = paginator.page(paginator.num_pages)

        # make the variable 'resources' available on the template
        context['resources'] = resources

        return context
Run Code Online (Sandbox Code Playgroud)

在模板中,您现在可以使用循环遍历项目{% for resource in resources %},并显示分页链接,如下所示:

<ul class="pagination">
  {% if resources.has_previous %}
    <li><a href="?page={{ resources.previous_page_number }}"><i class="fa fa-angle-left"></i></a></li>
  {% endif %}
  {% for page_num in resources.paginator.page_range %}
    <li {% if page_num == resources.number %}class="active"{% endif %}><a href="?page={{ page_num }}">{{ page_num }}</a></li>
  {% endfor %}
  {% if resources.has_next %}
    <li><a href="?page={{ resources.next_page_number }}"><i class="fa fa-angle-right"></i></a></li>
  {% endif %}
</ul>
Run Code Online (Sandbox Code Playgroud)

  • 只是想补充说我在最后一个`<li> </ li>`周围使用了以下内容,只显示下一个箭头,如果有下一页 - "{%if resources.has_next%} {%endif%} (4认同)

小智 5

我非常感谢你让我来到这里 - 非常感谢你的帮助。我必须做出一些调整才能使其发挥作用。如果有人遇到同样的问题,这里是模型:

class NewsIndexPage(Page):
intro = RichTextField(blank=True)

def get_context(self, request):
    context = super(NewsIndexPage, self).get_context(request)

    # Get the full unpaginated listing of resource pages as a queryset -
    # replace this with your own query as appropriate
    blogpages = self.get_children().live().order_by('-first_published_at')

    paginator = Paginator(blogpages, 3) # Show 3 resources per page

    page = request.GET.get('page')
    try:
        blogpages = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        blogpages = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        blogpages = paginator.page(paginator.num_pages)

    # make the variable 'resources' available on the template
    context['blogpages'] = blogpages

    return context
Run Code Online (Sandbox Code Playgroud)

...这是 HTML:

<ul class="pagination">
    {% if blogpages.has_previous %}
      <li>
        <a href="?page={{ blogpages.previous_page_number }}"><i class="fa fa-angle-left"></i></a>
      </li>
    {% endif %}
    {% for page_num in blogpages.paginator.page_range %}
      <li {% if page_num == blogpages.number %} class="active"{% endif %}>
        <a href="?page={{ page_num }}">{{ page_num }}</a>
      </li>
    {% endfor %}
    {% if resources.has_next %}
      <li>
        <a href="?page={{ blogpages.next_page_number }}"><i class="fa fa-angle-right"></i></a>
      </li>
      {% endif %}
  </ul>
Run Code Online (Sandbox Code Playgroud)

它就像一个魅力 - 并且增加了学习曲线!