django pagination 分页笔记
django pagination 分页笔记
酒瓶不倒 发表于2年前
django pagination 分页笔记
  • 发表于 2年前
  • 阅读 328
  • 收藏 3
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: 最近再学习django,下面是分页功能, 做个笔记。django pagination 分页,django 1.8.3 python 2.7.10
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 %}


共有 人打赏支持
粉丝 10
博文 77
码字总数 18414
×
酒瓶不倒
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: