摘要
分页用于实现对内容的化简,可以分页查看
1.效果图
image.png2.配置urls.py
urlpatterns = [
url(r'stupage/', views.stupage),
3.配置views.py
分页是采用python自带的Paginator包
from django.core.paginator import Paginator
def stupage(request):
if request.method == 'GET':
stus = Student.objects.all()
# 每页取多少条数据
paginator = Paginator(stus, 3)
# 取得url携带的page_id的页数, 默认取id=1
page_id = request.GET.get('page_id', 1)
page = paginator.page(int(page_id))
return render(request, 'index_page.html', {'stus': page})
4.####配置html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% for stu in stus %}
{# 第一次首页的时候, 数据库中没有数据, 添加完毕后, 数据中则有数据,从数据库中取出相应的数据#}
姓名: {{ stu.s_name }}
电话: {{ stu.s_tel }}
<br>
{% endfor %}
<h4>一共{{ stus.paginator.num_pages }}页/ 一共有{{ stus.paginator.count }} 条数</h4>
<h4>{% for i in stus.paginator.page_range %}
<a href="/stu/stupage/?page_id={{ i }}">{{ i }}</a>
{% endfor %}
</h4>
<h4>当前{{ stus.number }}页</h4>
{% if stus.has_previous %}
<a href="/stu/stupage/?page_id={{ stus.previous_page_number }}">上一页</a>
{% endif %}
{% if stus.has_next %}
<a href="/stu/stupage/?page_id={{ stus.next_page_number }}">下一页</a>
{% endif %}
<h4>--------------------------------------</h4>
<a href="{% url 's:add' %}">添加学生</a>
{#<a href="/stu/addstu/">添加老师</a>#}
</body>
</html>
网友评论