• python---django中自带分页类使用


    请先看在学习tornado时,写的自定义分页类:思路一致;

    python---自定义分页类

    1.基础使用:

    后台数据获取:

    from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage#错误判断
    List_info
    = [] for i in range(1000): List_info.append("news%d"%i) def get_page(req): current_page = req.GET.get('p') paginator = Paginator(List_info,10) #传入总数据和每页显示的数据
      #含有属性:
      #per_page:每页显示条数上面的10
      #count数据总数
      #num_pages:总页数
      #page_range:总页数索引范围
      #page:page对象
    try: posts = paginator.page(current_page)
         #page对象含有方法:
         #has_next:判断是否有下一页
         #has_previous:判断是否有上一页
         #next_page_number:下一页页码
         #previous_page_number:上一页页码
         #number:当前页  注意:这个是属性,其他是方法,需要加上()
         #paginator:paginator对象 except PageNotAnInteger: posts
    = paginator.page(1) except EmptyPage: posts = paginator.page(paginator.num_pages) #num_pages数总页数,最后一页 return render(req,'get_page.html',{'posts':posts})

    前端代码:

        <div class="content">
            {% for item in posts %}
                <p>{{ item }}</p>
            {% endfor %}
        </div>
        <div>
            <span>
                {% if posts.has_previous %}  #模板中调用函数不要()
                    <a href="/page.html?p={{ posts.previous_page_number }}">prev</a>
                {% endif %}
                <span class="current">
                    Page {{ posts.number }} of {{ posts.paginator.num_pages }}
                </span>
                {% if posts.has_next %}
                    <a href="/page.html?p={{ posts.next_page_number }}">next</a>
                {% endif %}
            </span>
        </div>

    2.扩展内置分页:

    类修改:

    from django.core.paginator import Paginator
    
    class CustomPagimator(Paginator):
        def __init__(self,current_page,max_page_num,*args,**kwargs):
            self.current_page = int(current_page)  #当前页  #自定义变量
            self.max_page_num = max_page_num    #可以显示多少页  #自定义变量
            super(CustomPagimator,self).__init__(*args,**kwargs)
    
        def page_num_range(self):
            # self.num_pages 总页数
            part_num = int(self.max_page_num/2)
            if self.num_pages <= self.max_page_num:  #先判断总页数和运行显示的页数大小
                return range(1, self.num_pages + 1)
            if self.current_page <= part_num:  #再取半分析
                return range(1,self.max_page_num+1)
            elif self.current_page+part_num>= self.num_pages:
                return range(self.num_pages-self.max_page_num,self.num_pages+1)
            else:
                return range(self.current_page - part_num, self.current_page + part_num + 1)

    类调用:

    List_info = []
    for i in range(1000):
        List_info.append("news%d"%i)
    
    from 模块.自定义分页类文件 import 自定义分页类CustomPagimator
    def get_page(req): current_page
    = req.GET.get('p') paginator = CustomPagimator(current_page=current_page, max_page_num=5,object_list=List_info,per_page=10) #传入总数据和每页显示的数据 try: posts = paginator.page(current_page) except PageNotAnInteger: posts = paginator.page(1) except EmptyPage: posts = paginator.page(paginator.num_pages) #num_pages数总页数,最后一页 page_html = paginator.page_num_range() return render(req,'get_page.html',{'posts':posts,"page_html":page_html})

    前端使用:

        <div>
            <span>
                {% if posts.has_previous %}
                    <a href="/page.html?p={{ posts.previous_page_number }}">prev</a>
                {% endif %}
                <span class="page">
                    {% for i in page_html %}
                        <a href="/page.html?p={{ i }}">{{ i }}</a>
                    {% endfor %}
                </span>
                <span class="current">
                    Page {{ posts.number }} of {{ posts.paginator.num_pages }}
                </span>
                {% if posts.has_next %}
                    <a href="/page.html?p={{ posts.next_page_number }}">next</a>
                {% endif %}
            </span>
        </div>
  • 相关阅读:
    7.9 C++ STL算法
    7.8 C++容器适配器
    7.7 C++基本关联式容器
    Django项目静态文件加载失败问题
    Centos6.5安装mysql5.7详解
    使用Xshell上传下载文件
    linux中MySQL本地可以连接,远程连接不上问题
    Linux常用命令
    Linux环境安装python3
    python 字符串最长公共前缀
  • 原文地址:https://www.cnblogs.com/ssyfj/p/8679187.html
Copyright © 2020-2023  润新知