Mixin for Django generic views, how can object have a superclass?
所以背景细节,post是一个模型,我基本上是想创建一个博客,和视频中显示的一样。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | from django.views.generic import ListView, DetailView from models import Post class PublishedPostsMixin(object): def get_queryset(self): queryset = super(PublishedPostsMixin, self).get_queryset() return queryset.filter(published=True) class PostListView(PublishedPostsMixin, ListView): # PostListView takes default template name as `post_list.html`, # as list was the name it was assigned. model = Post template_name = 'blog/post_list.html' class PostDetailView(PublishedPostsMixin, DetailView): model = Post template_name = 'blog/post_detail.html' |
所以,如果你能看到,
诀窍在于
有关