class PostDetailView(CommonViewMixin, DetailView): queryset = Post.latest_posts() template_name = 'Template/new_style/Blog/detail.html' context_object_name = 'post' pk_url_kwarg = 'post_id' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) comment_list = Comment.get_by_target(self.request.path) count = comment_list.count() context.update({ 'comment_form': CommentForm, 'comment_list': comment_list, }) print(context) return context
def content_html(self): #直接渲染模板 from Blog.models import Post from Comment.models import Comment result='' if self.display_type == self.DISPLAY_HTML: result = self.content elif self.display_type == self.DISPLAY_LATEST: context={ 'posts':Post.latest_posts() } result = render_to_string('Template/default/Config/blocks/sidebar_posts.html',context) elif self.display_type == self.DISPLAY_HOT: context={ 'posts':Post.hot_posts() } result = render_to_string('Template/default/Config/blocks/sidebar_posts.html',context) elif self.display_type == self.DISPLAY_COMMENT: context={ 'comments':Comment.objects.filter(status=Comment.STATUS_NORMAL) } result = render_to_string('Template/default/Config/blocks/sidebar_comment.html', context)
class IndexView(CommonViewMixin, ListView): #首页 queryset = Post.latest_posts() context_object_name = 'post_list' template_name = 'Template/default/Blog/list.html'