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
posted @ 2019-12-03 13:50  最美的烟火  阅读(964)  评论(0编辑  收藏  举报