Django分頁器
1、views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
#views.py
from
django.shortcuts
import
render, HttpResponse
# Create your views here.
from
app01.models
import
*
from
django.core.paginator
import
Paginator, EmptyPage, PageNotAnInteger
def
index(request):
obj_list
=
Book.objects.
all
()
paginator
=
Paginator(obj_list,
9
)
# 每頁顯示行數
try
:
current_page
=
int
(request.GET.get(
"page"
))
# 防止非數字
except
:
current_page
=
1
#########################################################
show_page
=
11
#默認顯示頁數
half
=
int
((show_page
-
1
)
/
2
)
all_pager
=
paginator.num_pages
#總頁數
# 若是數據總頁數 < 11
if
all_pager < show_page:
begin
=
1
stop
=
all_pager
+
1
# 若是數據總頁數 > 11
else
:
# 若是當前頁 <=5,永遠顯示1,11
if
current_page <
=
half:
begin
=
1
stop
=
show_page
+
1
else
:
if
current_page
+
half > all_pager:
begin
=
all_pager
-
show_page
+
1
stop
=
all_pager
+
1
else
:
begin
=
current_page
-
half
stop
=
current_page
+
half
+
1
page_range
=
range
(begin,stop)
#########################################################
try
:
obj_list
=
paginator.page(current_page)
except
PageNotAnInteger:
obj_list
=
paginator.page(
1
)
except
EmptyPage:
obj_list
=
paginator.page(paginator.num_pages)
# return render(request,"index.html",locals())
return
render(request,
"index.html"
, {
"obj_list"
: obj_list,
"current_page"
: current_page,
"page_range"
: page_range})
'''
批量導入數據:
Booklist=[]
for i in range(100):
Booklist.append(Book(title="book"+str(i),price=30+i*i))
Book.objects.bulk_create(Booklist)
分頁器的使用:
obj_list=Book.objects.all()
paginator = Paginator(obj_list, 10) #每頁顯示10條
print("count:",paginator.count) #數據總數
print("num_pages",paginator.num_pages) #總頁數
print("page_range",paginator.page_range) #頁碼的列表
page1=paginator.page(1) #第1頁的page對象
#顯示某一頁具體數據的兩種方式:
#方式1
for i in page1: #遍歷第1頁的全部數據對象
print(i)
#方式2
print(page1.object_list) #第1頁的全部數據
page2=paginator.page(2)
print(page2.has_next()) #是否有下一頁
print(page2.next_page_number()) #下一頁的頁碼
print(page2.has_previous()) #是否有上一頁
print(page2.previous_page_number()) #上一頁的頁碼
#當前頁
current_page=int(request.GET.get("page",1))
obj_list=paginator.page(current_page)
for i in obj_list: #遍歷當前頁的全部數據對象
print(i)
# 拋錯
#page=paginator.page(12) # error:EmptyPage
#page=paginator.page("z") # error:PageNotAnInteger
'''
|
2、index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
<!
DOCTYPE
html>
<
html
lang="en">
<
head
>
<
meta
charset="UTF-8">
<
title
>Title</
title
>
<
link
rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</
head
>
<
body
>
<
div
class="container">
<
h4
>分頁器</
h4
>
<
ul
>
{% for row in obj_list %}
<
li
>{{ row.title }} -----{{ row.price }}</
li
>
{% endfor %}
</
ul
>
<
nav
aria-label="Page navigation">
<
ul
class="pagination" id="pager">
{% if obj_list.has_previous %}
<
li
class="previous"><
a
href="?page={{ obj_list.previous_page_number }}">上一頁</
a
></
li
>
{% else %}
<
li
class="previous disabled"><
a
href="">上一頁</
a
></
li
>
{% endif %}
{% for num in page_range %}
{% if num == current_page %}
<
li
class="item active"><
a
href="?page={{ num }}">{{ num }}</
a
></
li
>
{% else %}
<
li
class="item"><
a
href="?page={{ num }}">{{ num }}</
a
></
li
>
{% endif %}
{% endfor %}
{% if obj_list.has_next %}
<
li
class="next"><
a
href="?page={{ obj_list.next_page_number }}">下一頁</
a
></
li
>
{% else %}
<
li
class="next disabled"><
a
href="">下一頁</
a
></
li
>
{% endif %}
</
ul
>
</
nav
>
</
div
>
</
body
>
</
html
>
|
自定義分頁
1、utils.pager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
class
PageInfo(
object
):
def
__init__(
self
,current_page,all_count,per_page,base_url
=
'',show_page
=
7
):
"""
:param current_page:
:param all_count: 數據庫總行數
:param per_page: 每頁顯示行數
:return:
"""
self
.all_count
=
all_count
try
:
self
.current_page
=
int
(current_page)
except
Exception as e:
self
.current_page
=
1
self
.per_page
=
per_page
a,b
=
divmod
(
self
.all_count,per_page)
if
b:
a
=
a
+
1
self
.all_pager
=
a
self
.show_page
=
show_page
self
.base_url
=
base_url
def
start(
self
):
return
(
self
.current_page
-
1
)
*
self
.per_page
def
end(
self
):
return
self
.current_page
*
self
.per_page
def
pager(
self
):
# v = "<a href='/custom.html?page=1'>1</a><a href='/custom.html?page=2'>2</a>"
# return v
page_list
=
[]
half
=
int
((
self
.show_page
-
1
)
/
2
)
# 若是數據總頁數 < 11
if
self
.all_pager <
self
.show_page:
begin
=
1
stop
=
self
.all_pager
+
1
# 若是數據總頁數 > 11
else
:
# 若是當前頁 <=5,永遠顯示1,11
if
self
.current_page <
=
half:
begin
=
1
stop
=
self
.show_page
+
1
else
:
if
self
.current_page
+
half >
self
.all_pager:
begin
=
self
.all_pager
-
self
.show_page
+
1
stop
=
self
.all_pager
+
1
else
:
begin
=
self
.current_page
-
half
stop
=
self
.current_page
+
half
+
1
first
=
"<li><a href='%s?page=1'>首頁</a></li>"
%
(
self
.base_url,)
page_list.append(first)
if
self
.current_page <
=
1
:
prev
=
"<li class='previous disabled'><a href=''>上一頁</a></li>"
else
:
prev
=
"<li class='previous'><a href='%s?page=%s'>上一頁</a></li>"
%
(
self
.base_url,
self
.current_page
-
1
,)
page_list.append(prev)
for
i
in
range
(begin,stop):
if
i
=
=
self
.current_page:
temp
=
"<li class='item active'><a href='%s?page=%s'>%s</a></li>"
%
(
self
.base_url,i,i,)
else
:
temp
=
"<li class='item'><a href='%s?page=%s'>%s</a></li>"
%
(
self
.base_url,i,i,)
page_list.append(temp)
if
self
.current_page >
=
self
.all_pager:
nex
=
"<li class='next disabled'><a href='#'>下一頁</a></li>"
else
:
nex
=
"<li class='next'><a href='%s?page=%s'>下一頁</a></li>"
%
(
self
.base_url,
self
.current_page
+
1
,)
page_list.append(nex)
if
self
.all_pager >
0
:
last
=
"<li><a href='%s?page=%s'>尾頁</a></li>"
%
(
self
.base_url,
self
.all_pager)
else
:
last
=
"<li><a href='%s?page=%s'>尾頁</a></li>"
%
(
self
.base_url,
1
)
page_list.append(last)
total
=
"<li><span>共<b>%s</b>頁<b>%s</b>條數據</span></li>"
%
(
self
.all_pager,
self
.all_count)
page_list.append(total)
return
''.join(page_list)
|
2、views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#app01.views.py
from
django.shortcuts
import
render, HttpResponse
from
utils.pager
import
PageInfo
from
app01.models
import
*
def
index(request):
# ==========begin 頁碼
try
:
current_page
=
int
(request.GET.get(
"page"
))
# 防止非數字
except
:
current_page
=
1
# base_url=request.path_info
all_count
=
Book.objects.
all
().count()
page_info
=
PageInfo(current_page,all_count,
8
)
obj_list
=
Book.objects.
all
()[page_info.start():page_info.end()]
# =========end 頁碼
return
render(request,
'index.html'
,{
'obj_list'
:obj_list,
'page_info'
:page_info})
|
3、index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<!
DOCTYPE
html>
<
html
lang="en">
<
head
>
<
meta
charset="UTF-8">
<
title
>Title</
title
>
<
link
rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</
head
>
<
body
>
<
div
class="container">
<
h4
>自定義分頁</
h4
>
<
ul
>
{% for book in obj_list %}
<
li
>{{ book.title }} -----{{ book.price }}</
li
>
{% endfor %}
</
ul
>
<
nav
aria-label="Page navigation">
<
ul
class="pagination">
{{ page_info.pager|safe }}
</
ul
>
</
nav
>
</
div
>
</
body
>
</
html
>
|