标签:pre charset ret -- list end ota gen lang
1 from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger 2 USER_LIST = [] 3 for i in range(1,999): 4 temp = {‘name‘:‘root‘+str(i),‘age‘:i} 5 USER_LIST.append(temp) 6 def FenYe(request): 7 8 current_page = request.GET.get(‘p‘) 9 # Paginator对象 10 paginator = Paginator(USER_LIST, 10) 11 try: 12 # Page对象 13 posts = paginator.page(current_page) 14 15 except PageNotAnInteger: 16 posts = paginator.page(1) 17 except EmptyPage: 18 posts = paginator.page(paginator.num_pages) 19 20 return render(request, ‘fenye.html‘, {‘posts‘: posts})
fenye.html:
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>fenye1</title> 6 </head> 7 <body> 8 {% for item in posts %} 9 <li>{{ item.name }}--{{ item.age }}</li> 10 {% endfor %} 11 {% include ‘include/pager.html‘ %} 12 13 </body> 14 </html>
pager.html
1 {% if posts.has_previous %} 2 <a href="/fenye1?p={{ posts.previous_page_number }}">上一页</a> 3 {% else %} 4 <a href="#">上一页</a> 5 {% endif %} 6 {% for i in posts.paginator.pager_num_range %} 7 {% if i == posts.number %} 8 <a style="font-size: 30px;" href="/fenye1?p={{ i }}">{{ i }}</a> 9 {% else %} 10 <a href="/fenye1?p={{ i }}">{{ i }}</a> 11 {% endif %} 12 {% endfor %} 13 14 {% if posts.has_next %} 15 <a href="/fenye1?p={{ posts.next_page_number }}">下一页</a> 16 {% endif %} 17 <span> 18 {{ posts.number }}/{{ posts.paginator.num_pages }} 19 </span>
全部数据:USER_LIST,=》得出共有多少条数据
per_page: 每页显示条目数量
count: 数据总个数
num_pages:总页数
page_range:总页数的索引范围,如: (1,10),(1,200)
page: page对象(是否具有下一页;是否有上一页;)
# has_next 是否有下一页
# next_page_number 下一页页码
# has_previous 是否有上一页
# previous_page_number 上一页页码
# object_list 分页之后的数据列表,已经切片好的数据
# number 当前页
# paginator paginator对象
标签:pre charset ret -- list end ota gen lang
原文地址:https://www.cnblogs.com/GodLv/p/9574177.html