我的views.py
:
class CommentMixin(object):
def get_context_data(self, **kwargs):
context = super(MenuMixin, self).get_context_data(**kwargs)
context['comments'] = Comments.objects.filter(post=xxxxxxxxxxxxx).order_by('-pub_date')
return context
...
class PostDetailView(CommentMixin, generic.DetailView):
model = models.Post
PostDetail = PostDetailView.as_view()
PostList = PostList.as_view()
和comment.html
的片段:
{% for comment in comments %}
<article class="uk-comment">
<header class="uk-comment-header">
<img class="uk-comment-avatar" src="{%gravatar_url comment.email%}" alt="Avatar">
<h4 class="uk-comment-title">{{comment.author}}</h4>
<div class="uk-comment-meta">Dodano dnia {{comment.pub_date}}</div>
</header>
<div class="uk-comment-body">{{comment.content}}</div>
</article>
urls.py
的片段:
url(r'^post/(?P<slug>[\w\-_]+)/$', 'page.views.PostDetail', name='post-detail'),
我想通过该页面上的帖子替换xxxxxxxxxxxxx
。网址格式如下所示:
http://localhost:8000/post/my-post-name/
答案 0 :(得分:0)
DetailView
将对象存储在 self.object 中
在get_context_data
中,您可以在过滤器中使用它:
context['comments'] = Comments.objects.filter(post=self.object).order_by('-pub_date')