【发布时间】:2020-10-11 19:16:20
【问题描述】:
评论成功保存在 django 后台,但不会显示在实际网站上。
这是评论模型:
class comment(models.Model):
linkedpost = models.ForeignKey(Post, related_name="postcomments", on_delete=models.CASCADE)
commentauthor = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField(max_length=100)
date_posted = models.DateTimeField(default=timezone.now)
这是博客主页的 html 代码。 post for 循环遍历所有 post 对象并将它们打印出来。我创建了一个评论循环来遍历链接帖子的所有 cmets 并打印。是不是我的html代码有问题?
{% for post in posts %}
<article class="media content-section">
<img class="rounded-circle article-img" src="{{ post.author.profile.image.url }}">
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="{% url 'user-posts' post.author.username %}">{{ post.author }}</a>
<small class="text-muted">{{ post.date_posted|date:"F d, Y" }}</small>
</div>
<h2><a class="article-title" href="{% url 'post-detail' post.id %}">{{ post.title }}</a></h2>
<p class="article-content">{{ post.content }}</p>
<div>
<h2>Comments</h2>
{% for cmnts in linkedpost.postcomments %}
#<a class="mr-2" href="{% url 'user-posts' cmnts.author.username %}">{{ cmnts.commentauthor }}</a>
<small class="text-muted">{{ cmnts.date_posted|date:"F d, Y" }}</small>
<p class="article-content">{{ cmnts.body }}</p>
{% endfor %}
</div>
</div>
</article>
{% endfor %}
【问题讨论】:
-
应该是
{% for cmnts in post.postcomments.all %}