n5321 | 2025年3月27日 15:57

Tags: blog building


venv/Scripts/activate
cd myprojectdir
python manage.py runserver 8001
考虑改用django_filter来在页面中实现筛选过滤功能,暂时报错较多,未成功,但是应该是方向性的!

class PostAdminList(TagContextMixin, FilterView):
    model = Post
    template_name = 'blog/index_admin.html'
    context_object_name = 'posts'
    filterset_class = PostFilter  # 直接使用 django-filter
    paginate_by = 50
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # 传递筛选选项
        context['status_choices'] = Post._meta.get_field('status').choices
        context['categories'] = Category.objects.all()
        context['tags'] = Tag.objects.all()
        context['current_tag'] = self.request.GET.get('tag')
        # 获取已筛选的文章列表,用于提取时间筛选项
        posts = self.get_queryset()
        context['years'] = [year.year for year in posts.dates('created_on', 'year', order='DESC')]
        context['months'] = posts.dates('created_on', 'month', order='DESC')
        return context