context_object_name = 'posts'.
The template default name is ListView 'object_list'

from .models import Post,Category
from django.views.generic.list import ListView
from django.shortcuts import get_object_or_404

class PostCategory(ListView):
    model = Post
    template_name = 'cat.html'
    context_object_name = 'posts'
    def get_queryset(self):
        self.category = get_object_or_404(Category, pk=self.kwargs['pk'])
        return Post.objects.filter(category=self.category)

    def get_context_data(self, **kwargs):
        context = super(PostCategory, self).get_context_data(**kwargs)
        context['category'] = self.category
        return context

相关文章:

  • 2021-11-02
  • 2020-05-20
  • 2021-08-09
  • 2021-10-26
猜你喜欢
  • 2021-08-09
  • 2022-12-23
  • 2021-11-02
  • 2021-08-09
  • 2021-09-10
  • 2021-04-26
  • 2021-12-05
相关资源
相似解决方案