django ListView

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
原文地址:https://www.cnblogs.com/donghaoblogs/p/11976341.html