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 的值来判断当前页。

  • 相关阅读:
    mysql 内联接、左联接、右联接、完全联接、交叉联接 区别
    JS 时间字符串与时间戳之间的转换
    MySQL性能优化的最佳20条经验
    ++i 与 i++ 的区别
    === 与 == 区别
    SC命令创建和删除windows服务
    杂记
    linux 文件编程
    u-boot 启动过程
    简单冒泡法
  • 原文地址:https://www.cnblogs.com/nnylee/p/14486093.html
Copyright © 2011-2022 走看看