django pagination 分页笔记

pip install django-pagination

配置setting

INSTALLED_APPS = (        
            'pagination',  
                ) 
MIDDLEWARE_CLASSES = (  
             'pagination.middleware.PaginationMiddleware',  
        )
TEMPLATE_CONTEXT_PROCESSORS = (
    ###auto page
    "django.core.context_processors.auth",

)

views.py

from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
def puppet_admin_c_s(request):
    lines = puppet_admin.objects.order_by("-id") ##取总长度
    paginator = Paginator(lines, 10)  ##默认10行
    page = request.GET.get('page')
    try:
        show_lines = paginator.page(page)
    except PageNotAnInteger:
        show_lines = paginator.page(1)
    except EmptyPage:
        show_lines = paginator.page(paginator.num_pages)
    return render_to_response('puppet_c_s.html', RequestContext(request, {'puppet_admin': show_lines,}))

模板文件

puppet_c_s.html(关键模板代码)

 <div class="base03">                                        
   {% load pagination_tags %}                                       
   {% autopaginate puppet_admin 5 %}                                           
   <table class="table table-bordered">                                              
       <tr>                                                  
          <td>puppet 模块路径</td>                                                  
          <td>puppet file.conf</td>                                                  
          <td>puppet 主配置文件</td>                                                  
          <td>puppet master ip</td>                                              
       </tr>                                               
       {% for foo in puppet_admin %}                                                   
       <tr>                                                          
            <td>{{ foo.puppet_model_path }}</td>                                                          
            <td>{{ foo.puppet_files_server_path }}</td>                                                          
            <td>{{ foo.puppet_config_path }}</td>                                                          
            <td>{{ foo.puppet_server_ip }}</td>                                                   
       </tr>                                               
       {% endfor %}                                           
       </table>                                       
       {% paginate %}                                      
</div>


关键参数:

{% load pagination_tags %}                                       
{% autopaginate puppet_admin 5 %}
{% paginate %}


转载于:https://my.oschina.net/u/142602/blog/496530

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值