django分页三

作者: 赖三石 | 来源:发表于2017-07-18 10:52 被阅读0次

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 = '<a href="#">上一页</a>'
    else:
        prev = '<a href="/index/?p=%s">上一页</a>' % (current_page-1)
    page_html_list.append(prev)

    for j in range(start_index, end_index):
        if current_page == j:
            page_html = '<a class="active" href="/index/?p=%s">%s</a>' % (j, j)
        else:
            page_html = '<a href="/index/?p=%s">%s</a>' % (j, j)
        page_html_list.append(page_html)

    if current_page == page_num:
        nex = '<a href="#">下一页</a>'
    else:
        nex = '<a href="/index/?p=%s">下一页</a>' % (current_page+1)
    page_html_list.append(nex)
    
    return render(request, 'index.html', {'data': data, 'page': page_html_list})

template

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .active{
            background-color: #1b6d85;
        }
    </style>
</head>
<body>

<ul>
    {% for d in data %}
        <li>{{ d }}</li>
    {% endfor %}
</ul>

{% for i in page %}
{{ i|safe }}
{% endfor %}
</body>
</html>

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 = '<a href="#">上一页</a>'
    else:
        prev = '<a href="/index/?p=%s">上一页</a>' % (current_page-1)
    page_html_list.append(prev)

    for j in range(start_index, end_index):
        if current_page == j:
            page_html = '<a class="active" href="/index/?p=%s">%s</a>' % (j, j)
        else:
            page_html = '<a href="/index/?p=%s">%s</a>' % (j, j)
        page_html_list.append(page_html)

    if current_page == page_num:
        nex = '<a href="#">下一页</a>'
    else:
        nex = '<a href="/index/?p=%s">下一页</a>' % (current_page+1)
    page_html_list.append(nex)

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

相关文章

  • 分页-Django REST框架

    分页-Django REST框架 pagination.py 分页 Django提供了几个类来帮助您管理分页数据-...

  • Django-分页

    django-分页 一.django自带的分页 以读取UserInfo表为例 1.导入模块 from django...

  • django 使用原生sql及分页

    python3 django 使用原生sql及分页 django 使用原生sql及分页,类似于django res...

  • 分页

    分页 分页库Paginator的基本语法 django提供了分页的工具,存在于django.core中 Pagin...

  • 第20天,分页器

    本篇写了使用Django自有的分页器paginator的用法和自定制分页器 1.1 Django之分页功能 Dj...

  • django分页三

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

  • (14)Django - 分页功能

    Django已为开发者内置了分页功能,只需调用Django内置分页功能的函数即可实现数据分页功能。我们在Djang...

  • 分页显示

    说明: 分页  Django 提供了一些类实现管理数据分页,这些类位于 django/core/pagina...

  • Django分页

    我们要实现如下样子的分页界面 在不借助于第三方Django APP的情况下,仅仅使用Django框架提供的分页(P...

  • Django常用-分页

    分页 Django提供了数据分页的类,这些类被定义在django/core/paginator.py中。 类Pag...

网友评论

    本文标题:django分页三

    本文链接:https://www.haomeiwen.com/subject/iexckxtx.html