分页

一、paginator分页类

1.创建对象
      格式:Paginator(,每页显示数据的条数)
      返回值:分页对象
2.对象的属性
      count    分页对象那个的个数
      num_pages   页面总数
      page_range    页码的列表
3.方法
      page(num) 返回page对象,如果给定的页码不存在,则抛出异常

二、page对象

page对象:Paginator类实例化 返回对象 调用page属性,返回page对象
属性:
      1.object_list    当前页码上的所有数据
      2.number         当前页码值
      3.paginator    返回Paginator的对象

方法

1.has_next   是否有下一页
2.has_provious    判断是否有上一页
3.has_other_pages   是否有上一页或者下一页
4.next_page_bumber    返回下一页的页码
5.provious_page_number    返回上一页的页码
6.len    返回当前页数数据的个数

main.py

from django.core.paginator import Paginator
from django.http import HttpResponse
from django.shortcuts import render


def showStu(req):
    stu = Students.objects.all()
    pagination = Paginator(stu,5)
    try:
        nowPage = int(req.GET.get('page'))
    except:
        nowPage = 1
    page = pagination.page(nowPage)
    return render(req,'stushow.html',{'page':page})

base.html



  
    
    
    
    
    Bootstrap 101 Template

    
    

    
    
    
  
  
{#  #################}
    {% block con %}
        
{% block pagecontent %} {% endblock %}
{% endblock %} {# ###############}

page.html

{% extends 'common/base.html' %}
{% block pagecontent %}
    
        {% for row in page.object_list %}
            
        {% endfor %}
    
ID 名字 性别 年龄
{{ row.id }} {{ row.sname }} {{ row.ssex }} {{ row.sage }}
{% endblock %}
image

你可能感兴趣的:(分页)