评论回复仅添加到特定评论

时间:2019-05-28 03:38:57

标签: python django

我正在编写一个Webapp,并且想添加评论/回复功能。这些评论效果很好,但是在尝试添加回复时,即使从其他评论中添加,所有回复也仅保存到第一个评论中。

我想将回复链接到其他评论。

使用ForeignKey()关系和related_manager将回复链接到特定注释。 我尝试了一个for循环,以从模板中的Comment()模型获取每个评论,尽管它可以很好地呈现每个评论,但似乎无法将它们链接到其特定回复。

# models.py

class Comment(models.Model):
    article = models.ForeignKey(Article, on_delete=models.CASCADE)
    name = models.CharField(max_length=400)
    email = models.EmailField()
    text = models.TextField()
    time_stamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return "Comment %s by %s" %(str(self.text)[:20], self.name)



class ReplyComment(models.Model):
    comment_reply = models.ForeignKey(Comment, on_delete=models.CASCADE, related_name="replies")
    name_reply = models.CharField(max_length=400)
    text_reply = models.TextField()
    time_stamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return "Reply by %s to %s" %(self.name_reply, str(self.comment_reply))

# views.py
def article(request, slug):
   article = Article.objects.get(slug=slug)
   comments = Comment.objects.filter(article=article)

    # comment form
   if request.method == 'POST':
      form = CommentForm(request.POST or None)
      reply_form = ReplyCommentForm(request.POST or None)

      if form.is_valid():
         text = form.cleaned_data['text']
         email = form.cleaned_data['email']
         name = form.cleaned_data['name']

            # create the comment
         comment = Comment(article=article, text=text,
                              email=email, name=name)
         comment.save()

      if reply_form.is_valid():
         name_reply = reply_form.cleaned_data['name_reply']
         text_reply = reply_form.cleaned_data['text_reply']
         comment = Comment.objects.get(id=request.POST.get('comment_id'))
         reply_comment = ReplyComment(name_reply=name_reply, text_reply=text_reply, comment_reply=comment)
         reply_comment.save()



   else:
      form = CommentForm()
      reply_form = ReplyCommentForm()

   context = {'article': article, 'form': form, 'reply_form' : reply_form, 'comments': comments}

   return render(request, 'main/article.html', context)
<!-- Comment Section -->
<section class="section scrollspy">
    <div class="container">
        <ul class="collection">
            {% for comment in comments %}
            <li class="collection-item">
                <span class="secondary-content">{{ comment.time_stamp }}</span>
                <h5 class="title">{{ comment.name }}</h5>
                <p>{{ comment.text }}</p>

                <a href="#reply-modal" class="modal-trigger" data-target="reply-modal"><i
                        class="material-icons">reply</i></a>
                <div class="modal" id="reply-modal">
                    <div class="modal-content">
                        <form action="" method="post">
                            <input type="hidden" name="comment_id" value="{{ comment.id }}">
                            {% csrf_token %}
                            <div class="row">
                                <p>Add a Reply</p>

                                <div class="input-field col s6">
                                    {{ reply_form.name_reply }}
                                </div>

                                <div class="input-field col s12">
                                    {{ reply_form.text_reply }}
                                </div>
                            </div>
                            <button type="submit" class="black white-text btn-small waves-effect">Reply</button>
                        </form>
                    </div>

                </div>

                <div class="container">
                    {% for reply in comment.replies.all %}

                    {{ reply.name_reply }}<br>
                    {{ reply.text_reply }}<br><br>

                    {% endfor %}
                </div>

            </li>
            {% endfor %}
        </ul>
    </div>
</section>

我希望添加的每个回复都将链接到他们的特定评论并如此链接。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

问题在于,模态元素上的ID停留在单个注释上,因此每个注释都呈现相同的模态ID。我通过将{{comment.id}}添加到ID使其动态来解决了该问题。非常感谢您的回复。

<a href="#reply-modal{{ comment.id }}" class="modal-trigger" data-target="reply-modal{{ comment.id }}"><i class="material-icons">reply</i></a>
       <div class="modal" id="reply-modal{{ comment.id }}">
            <div class="modal-content">