Wag的分页

时间:2016-11-01 17:36:57

标签: django pagination blogs 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>

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

3 个答案:

答案 0 :(得分:17)

Django为此目的提供了模块django.core.paginatorhttps://docs.djangoproject.com/en/1.10/topics/pagination/。在Wagtail中使用它与Django文档中的示例非常相似 - 唯一真正的区别在于,当您设置要传递给模板的Paginator对象时,使用{{1}进行此操作页面模型上的方法,而不是视图函数。您的模型定义将如下所示:

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 循环播放项目,并按如下方式显示分页链接:

{% for resource in resources %}

答案 1 :(得分:4)

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

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

...这里是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>

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

答案 2 :(得分:0)

万一它对任何人都有用,我希望它与class-based view ListView尽可能地接近,因此我得出了这样的结论:

from django.core.paginator import Paginator, InvalidPage
from django.http import Http404
from django.utils.translation import gettext as _

from wagtail.core.models import Page

class ArticleListPage(Page):

    # Some Page variables set here. #

    # Pagination variables:
    paginator_class = Paginator
    paginate_by = 10
    page_kwarg = 'page'
    paginate_orphans = 0
    allow_empty = False

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

        queryset = Page.objects.live()

        paginator, page, queryset, is_paginated = self.paginate_queryset(
                                        queryset, self.paginate_by, request)
        context.update({
            'paginator': paginator,
            'page_obj': page,
            'is_paginated': is_paginated,
            'object_list': queryset,
        })

        return context

    def paginate_queryset(self, queryset, page_size, request):
        """
        Adapted from the ListView class-based view.
        Added the request argument.
        """
        paginator = self.paginator_class(
                                    queryset,
                                    self.paginate_by,
                                    orphans=self.paginate_orphans,
                                    allow_empty_first_page=self.allow_empty)
        page_kwarg = self.page_kwarg
        page = request.GET.get(page_kwarg) or 1

        try:
            page_number = int(page)
        except ValueError:
            if page == 'last':
                page_number = paginator.num_pages
            else:
                raise Http404(_("Page is not 'last', nor can it be converted to an int."))
        try:
            page = paginator.page(page_number)
            return (paginator, page, page.object_list, page.has_other_pages())
        except InvalidPage as e:
            raise Http404(_('Invalid page (%(page_number)s): %(message)s') % {
                'page_number': page_number,
                'message': str(e)
            })

这将为您提供模板中与常规Django ListView相同的paginatorpage_objis_paginatedobject_list变量。

(使用python 3,Django 2.1和Wagtail 2.3。)