分页

from django.shortcuts import render
from app01 import models
# Create your views here.


def book_list(request):
    # 1. 查询所有的书籍数
    data = models.Book.objects.all()
    # 统计下总数据条数
    total_num = data.count()
    # 从URL中提取出来当前访问的具体页码数
    try:
        current_page = int(request.GET.get("page"))
    except Exception as e:
        current_page = 1
    print(current_page)
    # 定义每一页显示10条
    per_page = 10
    # 定义页面上最多显示多少个页码
    show_page_num = 7

    half_show_page_num = show_page_num // 2

    # 当前数据总共需要多少页码
    total_page, more = divmod(total_num, per_page)   #total 为总页数, more 为余数)
    # 如果有余数,就把页码数+1
    if more:
        total_page += 1
    # 如果当前页码数大于总页码数,默认展示最后一页的数据
    # current_page = total_page if current_page > total_page else current_page
    if current_page > total_page:
        current_page = total_page
    # 如果当前页码数小于1,默认展示第一页的数据
    if current_page < 1:
        current_page = 1

    # 求 页面上 需要展示的页码范围
    if current_page - half_show_page_num <= 1:
        page_start = 1
        page_end = show_page_num
    elif current_page + half_show_page_num >= total_page:
        page_end = total_page
        page_start = total_page - show_page_num + 1
    else:
        page_start = current_page - half_show_page_num
        page_end = current_page + half_show_page_num

    print("{} -{}- {}".format(page_start, current_page, page_end))
    print("=" * 120)

    # 当前页码应该展示的书籍数据
    book_list = data[(current_page-1)*per_page:current_page*per_page]
    # 生成分页的页码
    """
            <li><a href="#">1</a></li>
            <li><a href="#">2</a></li>
            <li><a href="#">3</a></li>
            <li><a href="#">4</a></li>
            <li><a href="#">5</a></li>
    """
    li_list = []
    # 添加首页
    li_list.append('<li><a href="/book_list/?page=1">首页</a></li>')
    # 添加上一页
    if current_page <= 1:  # 没有上一页
        prev_html = '<li class="disabled"><a aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>'
    else:
        prev_html = '<li><a href="/book_list/?page={}" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>'.format(current_page-1)
    li_list.append(prev_html)
    for i in range(page_start, page_end+1):
        if i == current_page:
            tmp = '<li class="active"><a href="/book_list/?page={0}">{0}</a></li>'.format(i)
        else:
            tmp = '<li><a href="/book_list/?page={0}">{0}</a></li>'.format(i)
        li_list.append(tmp)
    # 添加下一页
    if current_page >= total_page:  # 表示没有下一页
        next_html = '<li class="disabled"><a aria-label="Previous"><span aria-hidden="true">&raquo;</span></a></li>'
    else:
        next_html = '<li><a href="/book_list/?page={}" aria-label="Previous"><span aria-hidden="true">&raquo;</span></a></li>'.format(current_page+1)
    li_list.append(next_html)
    # 添加尾页
    li_list.append('<li><a href="/book_list/?page={}">尾页</a></li>'.format(total_page))

    # 将生成的li标签 拼接成一个大的字符串
    page_html = "".join(li_list)
    # print(page_html)

    # 2. 在页面上展示出来
    return render(request, "book_list.html", {"book_list": book_list, "page_html": page_html})

 

posted @ 2018-07-25 14:19  C3的脚印  阅读(149)  评论(0编辑  收藏  举报