视图界面
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>