Skip to content

Commit

Permalink
为归档、分类、标签页面添加分页功能 (#87)
Browse files Browse the repository at this point in the history
* 使用类的继承属性,为归档、分类、归档页面添加分页功能

* 完善分页导航功能
  • Loading branch information
WilliamGWJ authored and yangxg committed Jul 21, 2019
1 parent 2e30fc1 commit 8e1bbd4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
21 changes: 6 additions & 15 deletions blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,8 @@ def archives(request, year, month):
return render(request, 'blog/index.html', context={'post_list': post_list})


class ArchivesView(ListView):
model = Post
template_name = 'blog/index.html'
context_object_name = 'post_list'

# 归档页面,继承IndexView,可以拥有分页功能,减少代码量
class ArchivesView(IndexView):
def get_queryset(self):
year = self.kwargs.get('year')
month = self.kwargs.get('month')
Expand All @@ -280,21 +277,15 @@ def category(request, pk):
return render(request, 'blog/index.html', context={'post_list': post_list})


class CategoryView(ListView):
model = Post
template_name = 'blog/index.html'
context_object_name = 'post_list'

# 分类页面,继承IndexView
class CategoryView(IndexView):
def get_queryset(self):
cate = get_object_or_404(Category, pk=self.kwargs.get('pk'))
return super(CategoryView, self).get_queryset().filter(category=cate)


class TagView(ListView):
model = Post
template_name = 'blog/index.html'
context_object_name = 'post_list'

# 标签页面,继承自IndexView
class TagView(IndexView):
def get_queryset(self):
tag = get_object_or_404(Tag, pk=self.kwargs.get('pk'))
return super(TagView, self).get_queryset().filter(tags=tag)
Expand Down
55 changes: 34 additions & 21 deletions templates/blog/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,46 @@ <h1 class="entry-title">
{% comment %}
完善的分页导航
{% endcomment %}
{% if is_paginated %}
<div class="pagination">
<ul>
{% if first %}
<li><a href="?page=1">1</a></li>
{% if not is_paginated %}
<a href="?page=1">首页</a>
<a href="?page=1" style="color: red">1</a>
<a href="?page=1">尾页</a>
{% endif %}
{% if left %}
{% if left_has_more %}
<li><span>...</span></li>
{% if is_paginated %}
<a href="?page=1">首页</a>
{% if page_obj.has_previous %}
<a href="?page={{ page_obj.previous_page_number }}">上一页</a>
{% endif %}
{% for i in left %}
<li><a href="?page={{ i }}">{{ i }}</a></li>
{% endfor %}
{% endif %}
<li class="current"><a href="?page={{ page_obj.number }}">{{ page_obj.number }}</a></li>
{% if right %}
{% for i in right %}
<li><a href="?page={{ i }}">{{ i }}</a></li>
{% endfor %}
{% if right_has_more %}
<li><span>...</span></li>
{% if first %}
<li><a href="?page=1">1</a></li>
{% endif %}
{% endif %}
{% if last %}
<li><a href="?page={{ paginator.num_pages }}">{{ paginator.num_pages }}</a></li>
{% if left %}
{% if left_has_more %}
<li><span>...</span></li>
{% endif %}
{% for i in left %}
<li><a href="?page={{ i }}">{{ i }}</a></li>
{% endfor %}
{% endif %}
<li class="current"><a href="?page={{ page_obj.number }}" style="color: red">{{ page_obj.number }}</a></li>
{% if right %}
{% for i in right %}
<li><a href="?page={{ i }}">{{ i }}</a></li>
{% endfor %}
{% if right_has_more %}
<li><span>...</span></li>
{% endif %}
{% endif %}
{% if last %}
<li><a href="?page={{ paginator.num_pages }}">{{ paginator.num_pages }}</a></li>
{% endif %}
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">下一页</a>
{% endif %}
<a href="?page={{ paginator.num_pages }}">尾页</a>
{% endif %}
</ul>
</div>
{% endif %}
{% endblock main %}

0 comments on commit 8e1bbd4

Please sign in to comment.