• Django 内置分页的简单使用


    1, 文档
    https://docs.djangoproject.com/en/1.11.1/topics/pagination/ 
    2,视图
    
    
      from django.core.paginator import Paginator,EmptyPage, PageNotAnInteger
       picture_list = Picture.objects.all().only("avatar").order_by("pk") # 注:查询数据一定要排序,否则会有错误警告(但不会影响使用)!
    # 使用django 自带分页 # contact_list = Contacts.objects.all() paginator = Paginator(picture_list, 12) # 每一页的数据个数 page = request.GET.get('page') try: picture_data = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. picture_data = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. picture_data = paginator.page(paginator.num_pages) return render(request,"picture.html",{"picture_data":picture_data})
    3,html
    {% for picture in picture_data %}
        {# Each "contact" is a Contact model object. #}
                <a href="{{ picture.avatar }}"><img src="{{ picture.avatar }}" alt="error" title="原图" height="420px" width="310px"></a>
    {% endfor %}
    
    <div class="pagination">
        <span class="step-links">
            {% if picture_data.has_previous %}
                <a href="?page=1">首页</a>
                <a href="?page={{ picture_data.previous_page_number }}">上一页</a>
            {% endif %}
    
            {% if picture_data.has_next %}
                <a href="?page={{ picture_data.next_page_number }}">下一页</a>
                <a href="?page={{ picture_data.paginator.num_pages }}">尾页</a>
            {% endif %}
    
            <span class="current">
                第{{ picture_data.number }}页 ,一共 {{ picture_data.paginator.num_pages }} 页.
            </span>
        </span>
    </div>

  • 相关阅读:
    异常处理
    组合,封装
    自我介绍
    27python更多实例
    28python类代码编写细节
    29python运算符重载
    30python 类的设计
    31python类的高级主题
    32python异常基础
    33python异常编码细节
  • 原文地址:https://www.cnblogs.com/glf1160/p/10517216.html
Copyright © 2020-2023  润新知