修复对评论部分的答复,因为它显示为新评论

时间:2020-07-01 15:16:22

标签: python django django-views

在我的项目中,我有一个评论部分,该部分工作正常,但是现在我尝试添加对这些评论的回复,因为我知道这只是评论的另一条评论,所以每次添加时都会变得有些复杂对评论的回复,它显示为新评论,而不是回复。

我认为该错误可能在“视图”部分中,但我不知道如何解决。

这是模型。py

class Comment(models.Model):
    post = models.ForeignKey(
        Post, on_delete=models.CASCADE)
    user = models.ForeignKey(
        User, on_delete=models.CASCADE)
    reply = models.ForeignKey(
        'Comment', on_delete=models.CASCADE, null=True, related_name="replies")
    content = models.TextField(max_length=160)
    timestamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return '{} by {}'.format(self.post.title, str(self.user.username))

这是views.py

class PostDetailView(DetailView):
    model = Post
    template_name = "post_detail.html"

    def get_context_data(self, *args, **kwargs):
        context = super(PostDetailView, self).get_context_data()
        post = get_object_or_404(Post, slug=self.kwargs['slug'])
        comments = Comment.objects.filter(
            post=post, reply=None).order_by('-id')
        total_likes = post.total_likes()
        liked = False
        if post.likes.filter(id=self.request.user.id).exists():
            liked = True

        if self.request.method == 'POST':
            comment_form = CommentForm(self.request.POST or None)
            if comment_form.is_valid():
                content = self.request.POST.get('content')
                reply_id = self.request.POST.get('comment_id')
                comment_qs = None

                if reply_id:
                    comment_qs = Comment.objects.get(id=reply_id)
                comment = Comment.objects.create(
                    post=post, user=self.request.user, content=content, reply=comment_qs)
                comment.save()
                return HttpResponseRedirect("post_detail.html")
        else:
            comment_form = CommentForm()

        context["total_likes"] = total_likes
        context["liked"] = liked
        context["comments"] = comments
        context["comment_form"] = comment_form
        return context

这是模板

            <div class="container-fluid mt-2">
                <div class="form-group row">

                    <form action={% url 'score:post-comment' post.slug %} method="post" class="comment-form" action=".">
                    {% csrf_token %}
                    {{ comment_form.as_p }}
                    {% if request.user.is_authenticated %}
                    <input type="submit" value="Submit" class="btn btn-outline-success">
                    {% else %}
                    <input type="submit" value="Submit" class="btn btn-outline-success" disabled> You must be Logged in to Comment
                    {% endif %}
                    </form>
                </div>              
            </div>
            <div class="main-comment-section">
                {{comments.count}} Comment{{comments|pluralize}}
                {% for comment in comments %}
                <blockquote class="blockquote" style="border-left: .25rem solid #757f95;">
                    <p class="mb-0"> {{ comment.content}}</p>
                    <footer class="blockquote-footer">by<cite title="Source Title">{{comment.user| capfirst}}</cite></footer>
                </blockquote>

                        <!--A-->
                        <div class="replied-comments container mt-2">
                            {% for reply in comment.replies.all %}
                                <blockquote class="blockquote">
                                    <p class="mb-0"> {{ reply.content}}</p>
                                    <footer class="blockquote-footer">by<cite title="Source Title">{{reply.user| capfirst}}</cite></footer>
                                </blockquote>                            
                            {% endfor %}
                            <div class="form-group row">
                                <form action={% url 'score:post-comment' post.slug %} method="post" class="reply-form" >
                                {% csrf_token %}
                                <input type="hidden" name="comment_id" value="{{ comment.id }}">                    
                                {{ comment_form.as_p }}
                                {% if request.user.is_authenticated %}
                                <input type="submit" value="Submit" class="btn btn-outline-success">
                                {% else %}
                                <input type="submit" value="Submit" class="btn btn-outline-success" disabled> You must be Logged in to Comment
                                {% endif %}
                                </form>

                            </div>
                        </div>
                            <!--Z-->

0 个答案:

没有答案