django分页三

urls.py同前

views.py

# encoding=utf-8
from django.shortcuts import render

# Create your views here.
def index(request):
    #模拟数据,列表生成式
    a_list = [i for i in range(1, 1003)]

    #获取当前页, 注意获取时是字符串,要转换为int
    current_page = request.GET.get('p', 1)
    current_page = int(current_page)

    #数据切片
    #data = a_list[:10]
    #data = a_list[20: 30]

    #每页显示10条数据,并且得与current_page发生关系
    start = (current_page-1) * 10
    end = current_page * 10
    data = a_list[start: end]

    ########页码########
    #total_count:数据总条数
    #page_num:总页数
    total_count = len(a_list)
    page_num, y = divmod(total_count, 10)
    #y为余数,y=0为假
    if y:
        page_num += 1
############################################################
    # start_index = 1
    # end_index = page_num + 1

    # start_index = current_page - 5
    # end_index = current_page + 5 + 1

#解决页码变为负数问题
    if page_num <= 6:
        start_index = 1
        end_index = page_num + 1
    else:
        if current_page - 5 <= 0:
            start_index = 1
            end_index = current_page + 5 + 1
        else:
            if current_page + 5 + 1 > page_num:
                end_index = page_num + 1
                start_index = page_num - 10
            else:
                start_index = current_page - 5
                end_index = current_page + 5 + 1
#############################################################
#页码的前端显示

    page_html_list = []

    if current_page == 1:
        prev = '上一页'
    else:
        prev = '上一页' % (current_page-1)
    page_html_list.append(prev)

    for j in range(start_index, end_index):
        if current_page == j:
            page_html = '%s' % (j, j)
        else:
            page_html = '%s' % (j, j)
        page_html_list.append(page_html)

    if current_page == page_num:
        nex = '下一页'
    else:
        nex = '下一页' % (current_page+1)
    page_html_list.append(nex)
    
    return render(request, 'index.html', {'data': data, 'page': page_html_list})

template




    
    
    



    {% for d in data %}
  • {{ d }}
  • {% endfor %}
{% for i in page %} {{ i|safe }} {% endfor %}

views.py
提取 每页要显示数据数量 与 页码显示条数

# encoding=utf-8
from django.shortcuts import render

# Create your views here.
def index(request):
    #每页数据数量
    per_page_count = 8
    #显示页码数
    view_page = 9


    #模拟数据,列表生成式
    a_list = [i for i in range(1, 1003)]



    #获取当前页, 注意获取时是字符串,要转换为int
    current_page = request.GET.get('p', 1)
    current_page = int(current_page)

    #数据切片
    #data = a_list[:10]
    #data = a_list[20: 30]

    #每页显示10条数据,并且得与current_page发生关系
    start = (current_page-1) * per_page_count
    end = current_page * per_page_count
    data = a_list[start: end]

    ########页码########
    #total_count:数据总条数
    #page_num:总页数
    total_count = len(a_list)
    page_num, y = divmod(total_count, per_page_count)
    #y为余数,y=0为假
    if y:
        page_num += 1
############################################################
    # start_index = 1
    # end_index = page_num + 1

    # start_index = current_page - 5
    # end_index = current_page + 5 + 1

#解决页面变为负数问题
    if page_num <= (view_page+1)/2:
        start_index = 1
        end_index = page_num + 1
    else:
        if current_page - (view_page-1)/2 <= 0:
            start_index = 1
            end_index = current_page + (view_page-1)/2 + 1
        else:
            if current_page + (view_page-1)/2 + 1 > page_num:
                end_index = page_num + 1
                start_index = page_num - view_page + 1
            else:
                start_index = current_page - (view_page-1)/2
                end_index = current_page + (view_page-1)/2 + 1
################################################################


    page_html_list = []

    if current_page == 1:
        prev = '上一页'
    else:
        prev = '上一页' % (current_page-1)
    page_html_list.append(prev)

    for j in range(start_index, end_index):
        if current_page == j:
            page_html = '%s' % (j, j)
        else:
            page_html = '%s' % (j, j)
        page_html_list.append(page_html)

    if current_page == page_num:
        nex = '下一页'
    else:
        nex = '下一页' % (current_page+1)
    page_html_list.append(nex)

    return render(request, 'index.html', {'data': data, 'page': page_html_list})

你可能感兴趣的:(django分页三)