From 8e1bbd4b983ae947feb71c28415f99efcf6a32cd Mon Sep 17 00:00:00 2001 From: WilliamGWJ Date: Sun, 21 Jul 2019 11:41:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=E5=BD=92=E6=A1=A3=E3=80=81=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E3=80=81=E6=A0=87=E7=AD=BE=E9=A1=B5=E9=9D=A2=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=88=86=E9=A1=B5=E5=8A=9F=E8=83=BD=20(#87)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 使用类的继承属性,为归档、分类、归档页面添加分页功能 * 完善分页导航功能 --- blog/views.py | 21 +++++---------- templates/blog/index.html | 55 ++++++++++++++++++++++++--------------- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/blog/views.py b/blog/views.py index c6d14e5..71ddda9 100644 --- a/blog/views.py +++ b/blog/views.py @@ -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') @@ -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) diff --git a/templates/blog/index.html b/templates/blog/index.html index e53bede..4d82b11 100644 --- a/templates/blog/index.html +++ b/templates/blog/index.html @@ -54,33 +54,46 @@

{% comment %} 完善的分页导航 {% endcomment %} - {% if is_paginated %} - {% endif %} {% endblock main %} \ No newline at end of file