Django分页器
Posted 叨客厨子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django分页器相关的知识,希望对你有一定的参考价值。
Django分页器
当首页展示内容过多时,需要使用分页来讲数据传递给用户,这时就可以用到Django提供的分页器;
批量向数据库中插入数据
1.创建表:models.py
from django.db import models
class Book(models.Model):
name=models.CharField(max_length=32)
price=models.DecimalField(max_digits=7,decimal_places=2)
2.创建路由url:urls.py
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^add/', views.add),
]
3.批量创建数据:views.py
from django.shortcuts import render,HttpResponse,redirect
from app01 import models
def index(request):
pass
def add(request):
Booklist=[]
for i in range(100):
# 生成book对象
book_obj = models.Book(name="book"+str(i), price=30+i*i)
Booklist.append(book_obj)
# bulk_create 将实例化的 列表 插入到数据库中
models.Book.objects.bulk_create(Booklist)
return HttpResponse("OK")
启动后通过url:http://127.0.0.1:8000/add/ 添加数据
分页器Paginator模块的使用
1.Paginator模块功能介绍:
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
def index(request):
# 取出所有的书籍 100 条
book_list=models.Book.objects.all()
paginator=Paginator(book_list,10) # 表示每页展示的条数
print(paginator.count) # 统计一共有多少条数据
print(paginator.num_pages) # 表示一共多少页
print(paginator.page_range) # range(1, 11) 页数的范围值
page2=paginator.page(2) # 表示获取第二页的10条数据 book10--book19
# 根据当前页 判断是否有 上一页和下一页
print(page2.has_next()) # 是否有下一页
print(page2.next_page_number()) # 下一页的页码
print(page2.has_previous()) # 是否有上一页
print(page2.previous_page_number()) # 上一页的页码
输出:
100
10
range(1, 11)
True
3
True
1
分页器实现
1.views.py设置:
# 待改进,如果接收得字符为 字符串 会出错
from django.shortcuts import render,HttpResponse,redirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from app01 import models
from django.urls import reverse
# Create your views here.
def index(request):
Book_list = models.Book.objects.all()
paginator = Paginator(Book_list, 10)
page = request.GET.get('page', 1)
currentPage = int(page)
try:
Book_list = paginator.page(page)
except PageNotAnInteger:
Book_list = paginator.page(1)
except EmptyPage:
Book_list = paginator.page(paginator.num_pages)
return render(request, "index.html", {"Book_list": Book_list, "paginator": paginator, "currentPage": currentPage})
2.index.html设置:
<ul class="pagination" id="pager">
{% if Book_list.has_previous %}
<li class="previous"><a href="/index/?page=1">首页</a></li>
<li class="previous"><a href="/index/?page={{ Book_list.previous_page_number }}">上一页</a></li>
{% else %}
<li class="previous active"><a href="/index/?page=1">首页</a></li>
<li class="previous disabled"><a>上一页</a></li>
{% endif %}
{% for num in paginator.page_range %}
{% if num == currentPage %}
<li class="item active"><a href="/index/?page={{ num }}">{{ num }}</a></li>
{% else %}
<li class="item"><a href="/index/?page={{ num }}">{{ num }}</a></li>
{% endif %}
{% endfor %}
{% if Book_list.has_next %}
<li class="next"><a href="/index/?page={{ Book_list.next_page_number }}">下一页</a></li>
<li class="previous"><a href="/index/?page={{ paginator.num_pages }}">尾页</a></li>
{% else %}
<li class="next disabled"><a>下一页</a></li>
<li class="previous active"><a href="/index/?page={{ paginator.num_pages }}">尾页</a></li>
{% endif %}
</ul>
以上是关于Django分页器的主要内容,如果未能解决你的问题,请参考以下文章