django pagination 分頁筆記

pip install django-pagination

配置settinghtml

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

)

views.pypython

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,}))

模板文件shell

puppet_c_s.html(關鍵模板代碼)django

 <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>


關鍵參數:code

{% load pagination_tags %}                                       
{% autopaginate puppet_admin 5 %}
{% paginate %}
相關文章
相關標籤/搜索