定义:
InvalidPage:
page对象定义:
Page对象属性:
练习:
urls:
python">from django.urls import path
from . import views
urlpatterns = [
path('test_page',views.test_page)
]
views:
python">def test_page(request):
page_num = request.GET.get('page',1)
all_data = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v]','w','x','y','z']
# 初始化paginator对象
paginator = Paginator(all_data,2)
# 初始化具体页码的page对象
c_page = paginator.page(int(page_num))
return render(request,'node/test_page.html',locals())
templates.test_page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>分页</title>
</head>
<body>
{% for p in c_page %}
<p>
{{ p }}
</p>
{% endfor %}
{% if c_page.has_previous %}
<a href="/node/test_page?page={{ c_page.previous_page_number }}">上一页</a>
{% else %}
上一页
{% endif %}
{% for i in paginator.page_range %}
{% if i == c_page.number %}
{{ i }}
{% else %}
<a href="/node/test_page?page={{ i }}">{{ i }}</a>
{% endif %}
{% endfor %}
{% if c_page.has_next %}
<a href="/node/test_page?page={{ c_page.next_page_number }}">下一页</a>
{% else %}
下一页
{% endif %}
</body>
</html>
结果展示: