码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-03-31 00:50:46      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:索引   nbsp   gpo   super   lis   style   content   inf   exce   

请先看在学习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,PageNotAnInteger,EmptyPage

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.current_page+part_num>= self.num_pages:
            return range(self.num_pages-self.max_page_num,self.num_pages+1)
        elif self.current_page <= part_num:
            return range(1,self.max_page_num+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)

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>

 

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

标签:索引   nbsp   gpo   super   lis   style   content   inf   exce   

原文地址:https://www.cnblogs.com/ssyfj/p/8679187.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!