Django分页与Paginator

尉迟高澹
2023-12-01

分页

Django提供了一些类实现管理数据分页,这些类位于django/core/paginator.py中
Paginator对象

Paginator(列表,int):返回分页对象,参数为列表数据,每面数据的条数
属性

count:对象总数
num_pages:页面总数
page_range:页码列表,从1开始,例如[1, 2, 3, 4]
方法

page(num):下标以1开始,如果提供的页码不存在,抛出InvalidPage异常
异常exception

InvalidPage:当向page()传入一个无效的页码时抛出
PageNotAnInteger:当向page()传入一个不是整数的值时抛出
EmptyPage:当向page()提供一个有效值,但是那个页面上没有任何对象时抛出
Page对象

创建对象

Paginator对象的page()方法返回Page对象,不需要手动构造
属性

object_list:当前页上所有对象的列表
number:当前页的序号,从1开始
paginator:当前page对象相关的Paginator对象
方法

has_next():如果有下一页返回True
has_previous():如果有上一页返回True
has_other_pages():如果有上一页或下一页返回True
next_page_number():返回下一页的页码,如果下一页不存在,抛出InvalidPage异常
previous_page_number():返回上一页的页码,如果上一页不存在,抛出InvalidPage异常
len():返回当前页面对象的个数
迭代页面对象:访问当前页面中的每个对象
示例

直接上代码吧,先提供一下插入数据的sql语句吧。
insert into booktest_bookinfo(btitle,bpub_date,bread,bcommet,isDelete) values
-> (‘射雕英雄传’,’1980-5-1’,12,34,0),
-> (‘天龙八部’,’1986-7-24’,36,40,0),
-> (‘笑傲江湖’,’1995-12-24’,20,80,0),
-> (‘雪山飞狐’,’1987-11-11’,58,24,0);

insert into booktest_heroinfo(hname,hgender,hbook_id,hcontent,isDelete) values
-> (‘郭靖’,1,1,’降龙十八掌’,0),
-> (‘黄蓉’,0,1,’打狗棍法’,0),
-> (‘黄药师’,1,1,’弹指神通’,0),
-> (‘欧阳锋’,1,1,’蛤蟆功’,0),
-> (‘梅超风’,0,1,’九阴白骨爪’,0),
-> (‘乔峰’,1,2,’降龙十八掌’,0),
-> (‘段誉’,1,2,’六脉神剑’,0),
-> (‘虚竹’,1,2,’天山六阳掌’,0),
-> (‘王语嫣’,0,2,’神仙姐姐’,0),
-> (‘令狐冲’,1,3,’独孤九剑’,0),
-> (‘任盈盈’,0,3,’弹琴’,0),
-> (‘岳不群’,1,3,’华山剑法’,0),
-> (‘东方不败’,0,3,’葵花宝典’,0),
-> (‘胡斐’,1,4,’胡家刀法’,0),
-> (‘苗若兰’,0,4,’黄衣’,0),
-> (‘程灵素’,0,4,’医术’,0),
-> (‘袁紫衣’,0,4,’六合拳’,0);

定义模型。

from django.db import models

# Create your models here.
class UserInfo(models.Model):
    uname=models.CharField(max_length=10)
    upwd=models.CharField(max_length=40)
    isDelete=models.BooleanField()
class BookInfo(models.Model):
    btitle=models.CharField(max_length=20)
    bpub_date=models.DateTimeField(db_column='pub_date')
    bread=models.IntegerField(default=0)
    bcommet=models.IntegerField(null=False)
    isDelete=models.BooleanField(default=False)
class HeroInfo(models.Model):
    hname=models.CharField(max_length=10)
    hgender=models.BooleanField(default=True)
    hcontent=models.CharField(max_length=1000)
    isDelete=models.BooleanField(default=False)
    book=models.ForeignKey(BookInfo)

定义视图;

from django.shortcuts import render
from django.http import HttpResponse
import os
from django.conf import settings
from booktest.models import *
from django.core.paginator import Paginator

# Create your views here.
def index(request):
    return render(request,'booktest/index.html')
def MyExp(request):
    a=int('abc')
    return HttpResponse('hello')
def uploadPic(request):
    return render(request,'booktest/uploadPic.html')
def uploadHandle(request):
    pic1=request.FILES['pic1']
    #构造文件的存储路径
    picName=os.path.join(settings.MEDIA_ROOT,pic1.name)
    #打开这个文件;
    print(picName)

    with open(picName,'wb+') as pic:
        for c in pic1.chunks():
            pic.write(c)


    return HttpResponse('<img src=picName>')


def herolist(request,pindex):
    if pindex=='':
        pindex=1
    list=HeroInfo.objects.all()
    paginator=Paginator(list,5)
    page = paginator.page(int(pindex))
    context={'page':page}
    return  render(request,'booktest/herolist.html',context)

定义模板;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<ul>
    {% for hero in page %}
         <li>{{ hero.hname }}</li>
    {% endfor %}
</ul>
<hr>
{%for index in page.paginator.page_range %}
<a href="/herolist/{{ index }}" >{{ index }}</a>
{% endfor %}
</body>
</html>

定义url

from django.conf.urls import url
from booktest import views


urlpatterns=[
    url(r'^$',views.index),
    url(r'^myExp$',views.MyExp),
    url(r'^uploadPic',views.uploadPic),
    url(r'^uploadHandle$',views.uploadHandle),
    url(r'^herolist/(\d*)$',views.herolist)

]
 类似资料: