Skip to content

Commit 3a30ec1

Browse files
author
yangxg
committed
Step19: simple pagination
1 parent d141e88 commit 3a30ec1

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

blog/views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class IndexView(ListView):
3030
model = Post
3131
template_name = 'blog/index.html'
3232
context_object_name = 'post_list'
33+
paginate_by = 10
3334

3435

3536
"""

templates/blog/index.html

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,21 @@ <h1 class="entry-title">
2929
<div class="no-post">暂时还没有发布的文章!</div>
3030
{% endfor %}
3131

32-
<!-- 简单分页效果
33-
<div class="pagination-simple">
34-
<a href="#">上一页</a>
35-
<span class="current">第 6 页 / 共 11 页</span>
36-
<a href="#">下一页</a>
37-
</div>
38-
-->
32+
{% if is_paginated %}
33+
<div class="pagination-simple">
34+
<!-- 如果当前页还有上一页,显示一个上一页的按钮 -->
35+
{% if page_obj.has_previous %}
36+
<a href="?page={{ page_obj.previous_page_number }}">上一页</a>
37+
{% endif %}
38+
<!-- 显示当前页面信息 -->
39+
<span class="current">第 {{ page_obj.number }} 页 / 共 {{ paginator.num_pages }} 页</span>
40+
<!-- 如果当前页还有下一页,显示一个下一页的按钮 -->
41+
{% if page_obj.has_next %}
42+
<a href="?page={{ page_obj.next_page_number }}">下一页</a>
43+
{% endif %}
44+
</div>
45+
{% endif %}
46+
3947
<div class="pagination">
4048
<ul>
4149
<li><a href="">1</a></li>

0 commit comments

Comments
 (0)