django的分页器Paginator 从django中导入类
作者:谷子的 时间:2022-02-07 04:24:29
先创建表,然后生成批量数据。
在models文件里
from django.db import models
# Create your models here.
class Book(models.Model):
name = models.CharField(max_length=32)
price = models.DecimalField(max_digits=5,decimal_places=2)
然后执行python manage.py makemigrations ,python migrate 生成数据库。把数据库从左边拉到右边,
在url里创建showBooks视图函数API,
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', views.index),
url(r'^ajaxHandle/', views.ajaxHandle),
url(r'^showBooks/', views.showBooks),-------
在views文件中创建showBooks 函数,批量导入数据,用bulk_create()
从django中导入Paginstor类,用对象调用方法,
def showBooks(requests):
#批量导入数据bulk_create()方法
# book_list=[]#里面存一个个对象
# for i in range(100):
# book_list.append(Book(name="book%s"%i,price=2+i+2))
#
# Book.objects.bulk_create(book_list)
book_list_all = Book.objects.all()
#分页器Paginator,是导入了一个类,在用实列出来的对象调用方法,
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
#book_list_all 是要被分页的对象,第二个参数,是每页显示的条数
p = Paginator(book_list_all,20)# p就是每页的对象,
p.count #数据总数
p.num_pages #总页数
p.page_range#[1,2,3,4,5],得到页码,动态生成,
page_num = requests.GET.get("page")#以get的方法从url地址中获取
#如果输错了页码,
try:
book_list = p.page(page_num)#括号里的是页数,显示指定页码的数据,动态显示数据,所以不能写死了
except PageNotAnInteger:#如果输入页码错误,就显示第一页
book_list = p.page(1)
except EmptyPage:#如果超过了页码范围,就把最后的页码显示出来,
book_list = p.page(p.num_pages)
return render(requests,"showBooks.html",locals())
数据库生成数据
在templates 创建showBooks页面,把数据库数据渲染出来
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="{% static 'bootstrap/css/bootstrap.css' %}" rel="external nofollow" >
</head>
<body>
<ul>
{#request 也可以在这里渲染出来#}
{% for book in book_list %}
<li>{{ book.id }}     {{ book.name }}     {{ book.price }}</li>
{% endfor %}
</ul>
<ul class="pagination">
{% if book_list.has_previous %}
<li><a href="/showBooks/?page={{ book_list.previous_page_number }}" rel="external nofollow" >上一页</a></li> ---直接使用方法,上一页,
{% else %}
<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" >上一页</a></li>
{% endif %}
{% for num in p.page_range %}
<li><a href="/showBooks/?page={{ num }}" rel="external nofollow" >{{ num }}</a></li>
{% endfor %}
{% if book_list.has_next %}
<li><a href="/showBooks/?page={{ book_list.next_page_number }}" rel="external nofollow" >下一页</a></li>
{% else %}
<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" >下一页</a></li>
{% endif %}
</ul>
</body>
</html>
来源:https://www.cnblogs.com/gyh04541/p/7956109.html
标签:django,分页器,paginator,导入,类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
django redis的使用方法详解
2023-08-03 10:54:30
Python程序员鲜为人知但你应该知道的17个问题
2021-06-14 11:37:14
Python 解析库json及jsonpath pickle的实现
2021-09-10 00:27:14
![](https://img.aspxhome.com/file/2023/5/87925_0s.jpg)
pytorch dataset实战案例之读取数据集的代码
2023-10-06 23:51:01
![](https://img.aspxhome.com/file/2023/4/99204_0s.jpg)
Python实现的人工神经网络算法示例【基于反向传播算法】
2022-01-09 12:04:13
Python3实现的爬虫爬取数据并存入mysql数据库操作示例
2022-11-06 15:18:59
![](https://img.aspxhome.com/file/2023/1/66351_0s.png)
Python OpenCV基于霍夫圈变换算法检测图像中的圆形
2023-11-12 05:33:36
![](https://img.aspxhome.com/file/2023/8/64238_0s.jpg)
谷歌浏览器Chrome的javascript引擎
2008-09-04 12:24:00
![](https://img.aspxhome.com/file/UploadPic/20089/4/200894122755641s.gif)
常用于后台开发的jQuery插件
2010-09-25 12:47:00
Python实现判断一个整数是否为回文数算法示例
2022-05-13 12:01:07
![](https://img.aspxhome.com/file/2023/1/94181_0s.png)
Bootstrap图片轮播效果详解
2023-08-24 20:31:55
![](https://img.aspxhome.com/file/2023/1/56211_0s.jpg)
python检索特定内容的文本文件实例
2022-12-29 12:05:42
使用Dreamweaver便捷技巧方法十六则
2010-07-02 16:28:00
Python入门之集合的使用教程
2022-10-31 13:44:45
PHP常用字符串函数小结(推荐)
2023-06-14 00:18:50
在Python中处理日期和时间的基本知识点整理汇总
2021-05-13 07:12:14
![](https://img.aspxhome.com/file/2023/9/89079_0s.jpg)
python实现凯撒密码、凯撒加解密算法
2023-08-27 17:49:22
Python函数参数定义及传递方式解析
2021-10-19 21:28:25
Python3利用SMTP协议发送E-mail电子邮件的方法
2023-10-12 17:39:32
![](https://img.aspxhome.com/file/2023/2/63202_0s.jpg)
基于Python pyecharts实现多种图例代码解析
2021-11-10 05:20:17
![](https://img.aspxhome.com/file/2023/9/75939_0s.png)