Django 页码显示

视图界面

from django.shortcuts import render,HttpResponse
from app01 import models

# Create your views here.
def student_lst(request):
    # for i in range(200):
    #     student = models.Student()
    #     student.name = 'zs'+str(i)
    #     student.age = str(i)
    #     student.save()
    student_list=models.Student.objects.all()
    return render(request,'students.html',{'student_list':student_list})

def student_lst1(request):
    page=request.GET.get('page') # 获取页码
    page=int(page)  # 类型转换
    start_num=(page -1)*10 # 开始位置
    end_num=page * 10# 结束位置

    student_lst=models.Student.objects.all()[start_num:end_num]

    # 如果超出最大page 则不会显示内容--bug
    total_num=models.Student.objects.count()
    total_page_num=total_num/10 # 注意小数
    # total_page_num=20
    # total_page_num = math.ceil(total_page_num)
    # print(math.ceil(10/3)) # 3.333
    if total_page_num!=int(total_page_num):
        total_page_num+=1  # 注意小数
    total_page_num=int(total_page_num) # 整数
    if page> total_page_num:
        student_lst=models.Student.objects.all()[0:10]
        #5个导航数据
    if page<=3:
        start_page=1
        end_page_num=5
    else:
        start_page_num=page-2
        end_page_num=page +2
        if end_page_num>total_page_num:
            end_page_num=total_page_num

    page_range=range(start_page_num, end_page_num + 1)
    return render(request,'students.html',locals())

from  django.core.paginator import Paginator

def test(request):
    student_list=models.Student.objects.all().order_by('id')
    p=Paginator(student_list,10)
    count=Paginator.count
    ret=p.page(1)
    return HttpResponse('xxx')

网页页面:

    <table class="table table-bordered">
        <thead>
        <tr>
            <th>序号</th>
            <th>id</th>
            <th>姓名</th>
            <th>年龄</th>
        </tr>
        </thead>
        <tbody>

        {% for student in student_lst %}
            <tr>
                <td>{{ forloop.counter }}</td>
                <td>{{ student.id }}</td>
                <td>{{ student.name }}</td>
                <td>{{ student.age }}</td>

            </tr>
        {% endfor %}


        </tbody>
    </table>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值