码迷,mamicode.com
首页 > 其他好文 > 详细

Django分页器

时间:2017-11-29 16:20:49      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:imp   插入   分页器   render   创建表   ati   ble   blog   span   

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分页器

标签:imp   插入   分页器   render   创建表   ati   ble   blog   span   

原文地址:http://www.cnblogs.com/baolin2200/p/7920485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!