码迷,mamicode.com
首页 > 其他好文 > 详细

Django后端分页及前端显示效果

时间:2016-06-15 12:33:00      阅读:1050      评论:0      收藏:0      [点我收藏+]

标签:

1.导入分页相关模块

from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger

2.将对象分页,如下是按每页3条记录分页

customer_list = models.Customer.objects.all()

paginator = Paginator(customer_list,3)

翻到第1页:paginator.page(1)

翻到最后一页:paginator.page(paginator.num_pages)

当前页:customer_list.number

所有页内循环:for page_num in customer_list.paginator.page_range

3.前端控制

前端显示左翻页“<",页面数字,右翻页">"

技术分享
 1         <nav>
 2           <ul class="pagination">
 3             {% if customer_list.has_previous %}
 4                 <li class="">
 5                     <a href="?page={{ customer_list.previous_page_number }}" aria-label="Previous">
 6                         <span aria-hidden="true">&laquo;</span>
 7                     </a>
 8                 </li>
 9             {% endif  %}
10               {% for page_num in customer_list.paginator.page_range %}
11                    {%  guess_page customer_list.number page_num  %}
12             {% endfor %}
13            {% if customer_list.has_next %}
14                 <li class=""><a href="?page={{ customer_list.next_page_number }}" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>
15            {% endif %}
16           </ul>
17         </nav>
View Code

 自定义的函数,用于显示页面数字,当是当前时调用样式显示,否则无样式,如下:

技术分享
def guess_page(current_page,loop_num):
    offset = abs(current_page - loop_num)
    if offset <3:
        if current_page == loop_num:
            page_ele = ‘‘‘<li class="active"><a href="?page=%s">%s</a></li>‘‘‘ %(loop_num,loop_num)
        else:
            page_ele = ‘‘‘<li class=""><a href="?page=%s">%s</a></li>‘‘‘ %(loop_num,loop_num)
        return format_html(page_ele)
    else:
        return ‘‘
View Code

 

 

 

Django后端分页及前端显示效果

标签:

原文地址:http://www.cnblogs.com/lizheng19822003/p/5586927.html

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