我正在编写django博客应用程序,我在如何在Post视图中获取Comment对象时遇到问题。使用post对象没有问题,因为我写了<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-item nav-link active" href="#">Home <span class="sr-only">(current)</span></a>
<a class="nav-item nav-link" href="#">Features</a>
<a class="nav-item nav-link" href="#">Pricing</a>
<a class="nav-item nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</div>
</div>
</nav>
<div class="container-fluid no-gutters pl-0">
<div class="d-flex">
<div class="bg-dark d-none d-md-block side-panel" style="min-width: 18rem; max-width: 22rem;">
<ul class="list-group p-2">
<li class="list-group-item">
<a href="#" type="button" data-toggle="collapse" data-target="#multiCollapseExample2" aria-expanded="false" aria-controls="multiCollapseExample2">Parent - Test</a>
<ul class="list-group collapse" id="multiCollapseExample2">
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
<li class="list-group-item">Test</li>
</ul>
</li>
</ul>
</div>
<main role="main" class="flex-grow-1 pl-3 h-100">
Here is main content....
</main>
</div>
</div>
并且完成了。问题是我如何获得Comment对象。
下面是我的代码。
这是视图。
self.get_object()
这里是帖子模型:
class PostDetail(generic.DetailView, FormMixin):
template_name = 'post_detail.html'
context_object_name = 'post'
model = Post
form_class = CommentForm
这是评论模型
class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
text = RichTextUploadingField()
答案 0 :(得分:1)
我不知道您到底想做什么!但是,从被询问的内容来看,我认为一种方法是使用这样的基于函数的视图(我没有尝试过,但是我希望这样或那样做)非常适合您):
from django.shortcuts import render
def myview(request):
post_objs = Post.objects.all()
comment_objs = Comment.objects.all()
mydict = {'posts':post_obj, 'comments':comment_obj}
return render(request, 'myapp/myhtml.html', context=mydict)
考虑到您的一对多关系,您还可以进一步传递所需的确切数据
def myview(request, pk):
the_post_obj = Post.objects.get(pk=pk)
comment_objs = Comment.objects.filter(post=the_post_obj)
mydict = {'the_post':the_post_obj,'comments':comment_objs}
return render(request, 'myapp/myhtml.html', context=mydict)
使用您编写的任何视图,现在都可以转到HTML文件并以所需的任何方式显示查询的数据。例如,您可以显示单个帖子的所有评论。不要忘记,要使用我在上面写的第二个视图,必须将帖子PK发送到该视图(例如,可以将其放在HTML文件中的链接中,以便当用户单击该帖子时(即超链接),则帖子PK将通过查看功能,并显示相关数据)。
答案 1 :(得分:0)
您可以将DetailView更改为TemplateView并覆盖get_context_data()。参见:http://ccbv.co.uk/
或者您可以通过for循环在模板中获取与帖子相关的评论:
for comment in self.get_object().comments.all
# do sth with the comment
答案 2 :(得分:0)
您可以通过覆盖detailView中的get_context_data来获得注释对象,如下所示-
def get_context_data(self,** kwargs): context = super()。get_context_data(** kwargs) context ['comment'] = self.get_object()。comments.all() 返回上下文