Html 在Django主页上发表评论

Html 在Django主页上发表评论,html,django,django-models,django-rest-framework,django-views,Html,Django,Django Models,Django Rest Framework,Django Views,我随身携带这段代码,作为我在博客文章中遵循的教程的一部分。我在我的detailview上有一个评论部分,我希望它也能显示在主页上。我尝试了一些代码,但问题是相同的评论只显示在主页上的所有帖子 <!-- Comment showing section --> <div class="main-comment-section"> <div class="container-fluid mt-2"> <div class="form-g

我随身携带这段代码,作为我在博客文章中遵循的教程的一部分。我在我的detailview上有一个评论部分,我希望它也能显示在主页上。我尝试了一些代码,但问题是相同的评论只显示在主页上的所有帖子

<!-- 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>
请看一下代码

我有一个评论片段,我在我的主页上使用它

<!-- 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>

备注:评论正确显示在详细视图中,但在主页中,它显示了每个帖子中所有帖子的所有评论。

有以下更改

views.py

也更改posts/comment_section.html


你能更新你的问题以显示你的models.py并删除细节视图吗?我已经更新了帖子。你能显示你在主页中使用的帖子片段吗?没关系。这就是我添加到上述问题中的评论片段。你可以找到上面的模型。我补充了一个答案。
<!-- 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>

        {% if not post.comment_set.all %}
             No comments to display.
        {% endif %}

        {% for comment in post.comment_set.all %}
        <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>