【发布时间】:2020-09-04 18:07:15
【问题描述】:
我有这个代码作为我为博客文章关注的教程的一部分。我的详细视图上有一个评论部分,我希望它也显示在主页中。我尝试了一些代码,但问题是相同的 cmets 显示在主页的所有帖子中(仅)。
请看一下代码。
我有一条评论 sn-p 我在我的主页中使用它。
<!-- Comment showing section -->
<div class="main-comment-section">
<div class="container-fluid mt-2">
<div class="form-group row">
<!-- Comment Form -->
<form class="comment-form" method="post" action=".">{% csrf_token %}
<div class="form-group">
<textarea name="content" cols="60" rows="2" maxlength="1000" required="" id="id_content"></textarea>
</div>
<button type="submit" value="submit" class="btn-sm btn-outline-light" style="color: black;">Comment</button>
</form>
<!-- Comment Form end -->
</div>
</div>
{{ comments.count }} Comment{{ comments|pluralize }}
{% for comment in comments %}
<blockquote class="blockquote">
<img style="float:left; clear: left;" class="rounded-circle article-img" height="10" width="10" src="{{ comment.user.profile.profile_pic.url }}"><a href="" style="text-decoration: none; color: black;"><h6>{{ comment.user.first_name|capfirst }} {{ comment.user.last_name|capfirst }}</h6></a><br>
<p style="font-size: 8px;">{{ comment.timestamp }}</p>
<p style="font-size: 14px;" class="mb-3">{{ comment.content }}</p>
<a type="button" name="button" class="reply-btn ml-4"><p style="font-size: 13px;"> Reply</p></a>
{% if request.user == comment.user %}
<a href="{% url 'posts:delete_comment' comment.id %}" style="font-size: 13px;text-decoration: none; color: #000;" hover="background-color:red">Delete</a></td>
{% endif %}
</blockquote>
{{ comment.reply.count }}
<div class="replied-comments col-md-5" style="display: none;">
{% for reply in comment.replies.all %} <!--replies is the related name in the model-->
<blockquote class="blockquote">
<img style="float:left; clear: left;" class="rounded-circle article-img" height="50" width="50" src="{{ reply.user.profile.profile_pic.url }}"><a href="" style="text-decoration: none; color: black;"><h6>{{ reply.user.first_name|capfirst }} {{ reply.user.last_name|capfirst }}</h6></a><br>
<p style="font-size: 13px;" class="mb-3">{{ reply.content }}</p>
</blockquote>
{% endfor %}
<div class="form-group row">
<form class="reply-form" method="post" action=".">{% csrf_token %}
<input type="hidden" name="comment_id" value="{{ comment.id }}">
<div class="form-group">
<textarea name="content" cols="60" rows="2" maxlength="1000" required="" id="id_content"></textarea>
</div>
<input type="submit" value="submit" class="btn-sm btn-outline-light" style="color: black;">
</form>
</div>
</div>
{% endfor %}
</div>
PS。评论在详细视图中正确显示,但在主页中显示每个帖子中所有帖子的所有 cmets。
【问题讨论】:
-
您能否更新您的问题以显示您的 models.py 并删除详细视图?
-
我已经更新了帖子。
-
您能否也显示您在主页中使用的 Post sn-p?
-
没关系。这就是我在上面的问题中添加的评论 sn-p。你可以找到上面的模型。
-
我添加了一个答案。
标签: html django django-models django-rest-framework django-views