【发布时间】:2019-01-24 10:35:04
【问题描述】:
class PostDetailView(DetailView):
model = Post
template_name = 'detail.html'
def get_context_data(self, **kwargs):
context = super(PostDetailView, self).get_context_data(**kwargs)
instance = Post.objects.get(pk=self.kwargs.get('pk'))
user = instance.post_user
context['comments'] = Comment.objects.filter(comment_post=instance.pk)
context['comments']['profile'] = Profile.objects.get(user=user)
return context
这是我目前的看法。当我使用该代码时,我收到此错误“QuerySet”对象不支持项目分配。如何正确附加下面的行?
context['comments']['profile'] = Profile.objects.get(user=user)
【问题讨论】:
-
context['profile'] = Profile.objects.get(user=user)?? -
我正在尝试附加留下评论的用户。通过这样做,我将附加帖子的用户
-
但是你不附加留下评论的用户。
user始终是 post_user,因为您从不将其定义为其他任何东西。你确定你需要这样做吗?评论是否有指向创建它的用户的链接?你应该展示模型。
标签: python django django-class-based-views