文章目录
Django组件-分页器django自带的分页器自定制分页器
Django组件-分页器
django自带的分页器
增加测试数据 view.py(视图层)
...
book_list
= []
for i
in range(1, 101):
book_list
.append
(models
.Book
(title
="book_%s" % i
, price
=random
.randint
(20, 300)))
models
.Book
.objects
.bulk_create
(book_list
)
基本语法及参数 view.py(视图层)
from django
.core
.paginator
import Paginator
book_list
= models
.Book
.objects
.all()
paginator
= Paginator
(book_list
, 10)
print("count", paginator
.count
)
print("num_pages", paginator
.num_pages
)
print("page_range", paginator
.page_range
)
page1
= paginator
.get_page
(1)
for i
in page1
:
print(i
)
print(page1
.object_list
)
page2
= paginator
.get_page
(2)
print(page2
.has_next
())
print(page2
.next_page_number
())
print(page2
.has_previous
())
print(page2
.previous_page_number
())
page
=paginator
.page
(22)
page
=paginator
.page
("z")
基本使用 view.py视图层
from django
.shortcuts
import render
, redirect
from django
.views
import View
from app01
import models
from django
.core
.paginator
import Paginator
import random
class Index(View
):
def get(self
, request
):
book_list
= models
.Book
.objects
.all()
paginator
= Paginator
(book_list
, 10)
current_page
= int(request
.GET
.get
("page") if request
.GET
.get
("page") and request
.GET
.get
("page").isdigit
() else 1)
page
= paginator
.get_page
(current_page
)
if paginator
.num_pages
> 11:
if current_page
- 5 < 1:
page_range
= range(1, 12)
elif current_page
+ 5 > paginator
.num_pages
:
page_range
= range(paginator
.num_pages
- 10, paginator
.num_pages
+ 1)
else:
page_range
= range(current_page
- 5, current_page
+ 6)
else:
page_range
= paginator
.page_range
return render
(request
, "index.html", {"paginator": paginator
, "page": page
, "current_page": current_page
, "page_range": page_range
})
templates/index.html模板层
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<h3>书籍列表
</h3>
<ul>
{% for book in page %}
<li>{{ book.title }} --- {{ book.price }}
</li>
{% endfor %}
</ul>
<nav aria-label="Page navigation">
<ul class="pagination">
<li><a href="?page=1">首页
</a></li>
{% if page.has_previous %}
<li><a href="?page={{ page.previous_page_number }}">上一页
</a></li>
{% else %}
<li class="disabled"><a href="javascript:void(0);">上一页
</a></li>
{% endif %}
{% for num in page_range %}
{% if current_page == num %}
<li class="active"><a href="?page={{ num }}">{{ num }}
</a></li>
{% else %}
<li><a href="?page={{ num }}">{{ num }}
</a></li>
{% endif %}
{% endfor %}
{% if page.has_next %}
<li><a href="?page={{ page.next_page_number }}">下一页
</a></li>
{% else %}
<li class="disabled"><a href="javascript:void(0);">下一页
</a></li>
{% endif %}
<li><a href="?page={{ paginator.num_pages }}">尾页
</a></li>
</ul>
</nav>
</body>
</html>
自定制分页器
自定制分页器类 在APP下,新建一个py文件app01/page.py
class Paginator:
def __init__(self
, request
, current_page
, all_count
, per_page
=10, max_page_num
=13):
"""
封装分页相关数据
:param current_page: 当前页码
:param all_count: 数据库中的数据总条数
:param per_page: 每个页面显示的数据条数
:param max_page_num: 最多显示的页码个数
:param num_pages: 通过总条数/每个页面显示的条数,求出总页数
"""
try:
current_page
= int(current_page
)
except Exception
as e
:
current_page
= 1
if current_page
< 1:
current_page
= 1
self
.current_page
= current_page
self
.all_count
= all_count
self
.per_page
= per_page
num_pages
, temp
= divmod(all_count
, per_page
)
if temp
:
num_pages
+= 1
self
.num_pages
= num_pages
self
.max_page_num
= max_page_num
self
.page_count_half
= int((self
.max_page_num
- 1) / 2)
import copy
self
.url_args
= copy
.deepcopy
(request
.GET
)
print(self
.url_args
.urlencode
())
"""
self.num_pages=100
per_page=8
current_page =1 [0:8]
current_page =2 [8:16]
current_page =3 [16:24]
[(current_page-1)*per_page:current_page*per_page ]
"""
@
property
def start(self
):
return (self
.current_page
- 1) * self
.per_page
@
property
def end(self
):
return self
.current_page
* self
.per_page
def page_html(self
):
if self
.num_pages
<= self
.max_page_num
:
page_start
= 1
page_end
= self
.num_pages
+ 1
else:
if self
.current_page
<= self
.page_count_half
:
page_start
= 1
page_end
= self
.max_page_num
+ 1
elif self
.current_page
+ self
.page_count_half
> self
.num_pages
:
page_start
= self
.num_pages
- self
.max_page_num
+ 1
page_end
= self
.num_pages
+ 1
else:
page_start
= self
.current_page
- self
.page_count_half
page_end
= self
.current_page
+ self
.page_count_half
+ 1
page_html_list
= []
self
.url_args
['page'] = 1
first_page
= '<nav aria-label="Page navigation"><ul class="pagination"><li><a href="?%s">首页</a></li>' % (self
.url_args
.urlencode
())
page_html_list
.append
(first_page
)
if self
.current_page
<= 1:
prev_page
= '<li class="disabled"><a href="javascript:void(0);">上一页</a></li>'
else:
self
.url_args
['page'] = self
.current_page
- 1
prev_page
= '<li><a href="?%s">上一页</a></li>' % (self
.url_args
.urlencode
(), )
page_html_list
.append
(prev_page
)
for i
in range(page_start
, page_end
):
self
.url_args
['page'] = i
if self
.current_page
== i
:
temp
= '<li class="active"><a href="?%s">%s</a></li>' % (self
.url_args
.urlencode
(), i
)
else:
temp
= '<li><a href="?%s">%s</a></li>' % (self
.url_args
.urlencode
(), i
)
page_html_list
.append
(temp
)
if self
.current_page
>= self
.num_pages
:
next_page
= '<li class="disabled"><a href="javascript:void(0);">下一页</a></li>'
else:
self
.url_args
['page'] = self
.current_page
+ 1
next_page
= '<li><a href="?%s">下一页</a></li>' % (self
.url_args
.urlencode
(), )
page_html_list
.append
(next_page
)
self
.url_args
['page'] = self
.num_pages
last_page
= '<li><a href="?%s">尾页</a></li></ul></nav>' % self
.url_args
.urlencode
()
page_html_list
.append
(last_page
)
return "".join
(page_html_list
)
view.py视图层
from django
.shortcuts
import render
, HttpResponse
from django
.views
import View
from app01
import models
import random
from app01
.page
import Paginator
class BookList(View
):
def get(self
, request
):
book_list
= models
.Book
.objects
.all()
current_page
= int(request
.GET
.get
("page") if request
.GET
.get
("page") and request
.GET
.get
("page").isdigit
() else 1)
paginator
= Paginator
(request
, current_page
, book_list
.count
(), 5, 11)
book_list
= book_list
[paginator
.start
: paginator
.end
]
return render
(request
, "book_list1.html", {"book_list": book_list
, "paginator": paginator
, "current_page": current_page
})
templates/index.html模板层
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<h3>书籍列表!!!
</h3>
<ul>
{% for book in book_list %}
<li>{{ book.title }} --- {{ book.price }}
</li>
{% endfor %}
</ul>
{{ paginator.page_html|safe }}
</body>
</html>