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

  • 相关阅读:
    删除重复记录
    SQL Server调试存储过程
    SQL日期格式化应用大全
    阻塞分析
    Ajax原理详细说明
    varchar和nvarchar的区别
    临时表vs.表变量以及它们对SQLServer性能的影响
    Enterprise Library系列文章回顾与总结
    关于分页控件的文章
    SQL操作全集
  • 原文地址:https://www.cnblogs.com/nnylee/p/14486093.html
Copyright © 2011-2022 走看看