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),
    url(r'^$', views.index, name="INDEX"),
    url(r'^index/', views.index,name="INDEX"),
]

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, 15)
    page = request.GET.get('page',"1")
    # 如果获取的值不为数字,把所有请求跳转到首页
    if page.isdigit():
        currentPage=int(page)
    else:
        currentPage = int(1)
    #  如果页数十分多时,可以使用固定显示页码个数
    if paginator.num_pages>30:

        if currentPage-5<1:
            pageRange=range(1,11)
        elif currentPage+5>paginator.num_pages:
            pageRange=range(currentPage-5,paginator.num_pages+1)

        else:
            pageRange=range(currentPage-5,currentPage+5)

    else:
        pageRange=paginator.page_range

    print("==============",pageRange)
    try:
        print(page)
        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",locals())

2.index.html设置:

   <ul class="pagination" id="pager">
                 {% if Book_list.has_previous %}
                    <li class="next"><a href="/index/?page=1">首页</a></li>
                    <li class="previous"><a href="/index/?page={{ Book_list.previous_page_number }}">上一页</a></li>

                 {% else %}
                    <li class="next active"><a href="/index/?page=1">首页</a></li>
                    <li class="previous disabled"><a>上一页</a></li>
                 {% endif %}

                 {# 获取 range(1, 11) 表示一个循环值 #}
                 {% for num in pageRange %}

                     {% 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="next"><a href="/index/?page={{ paginator.num_pages }}">尾页</a></li>
                 {% else %}
                    <li class="next disabled"><a>下一页</a></li>
                    <li class="next active "><a href="/index/?page={{ paginator.num_pages }}">尾页</a></li>
                 {% endif %}
   </ul>
posted @ 2017-11-29 14:47  叨客厨子  阅读(212)  评论(0编辑  收藏  举报