zoukankan      html  css  js  c++  java
  • Wagtail 分页

    前言

    Wagtail 一些功能(例如 search )自带有分页实例,简单使用了下,记于此。

    实现

    笔者是在 model 的 get_context 下使用分页,核心是 Paginator 类

    def get_context(self, request):
        context = super().get_context(request)
        wikipages = self.get_children().live().order_by('-first_published_at')
    
        # Pagination
        paginator = Paginator(wikipages, 20)  # Show 20 resources per page
        page = request.GET.get('page')
        try:
            wikipages = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            wikipages = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of results.
            wikipages = paginator.page(paginator.num_pages)
    
        context['wikipages'] = wikipages
        return context
    

    模板中使用:

    ...
    {% if wikipages.has_previous %}
        <a class="page-link" href="?page={{ wikipages.previous_page_number }}">上一页</a>
    {% endif %}
    
    {% if wikipages.has_next %}
        <a class="page-link" href="?page={{ wikipages.next_page_number }}">下一页</a>
    {% endif %}
    

    总结

    使用了 django 提供的 Paginator 类完成分页,在 url 接受一个 page 参数,并根据 page 的值来判断当前页。

  • 相关阅读:
    远程连接telnet和ssh的区别?(telnet如何连接)
    NFS实践(搭建页面)
    NFS挂载 卸载
    NFS实践
    03 Linux 文件管理
    02 bashshell介绍使用
    01 Linux 的渊源与发展史
    P4218 [CTSC2010]珠宝商
    P5284 [十二省联考2019]字符串问题
    广义后缀自动机(广义SAM)
  • 原文地址:https://www.cnblogs.com/nnylee/p/14486093.html
Copyright © 2011-2022 走看看