我的评论回复功能不起作用?

时间:2019-03-05 06:07:03

标签: django django-models django-forms django-comments

我正在创建博客,但是我停留在评论回复功能上。我不知道如何创建一个“回复”功能,用户可以从中回复评论。我做了很多尝试,但是它仍然像其他评论一样显示这些评论,而不是对特定评论的答复。

这是模型。py

class Comment(models.Model):
    post = models.ForeignKey(Post,on_delete=models.CASCADE,related_name = "comments")
    name = models.CharField(max_length = 200)
    body = models.TextField(default = True)
    pub_date = models.DateTimeField(auto_now_add = True)
    reply = models.ForeignKey('Comment',null = True,blank = True,on_delete=models.CASCADE)


    class Meta:
        ordering = ['-pub_date']



    def __str__(self):
       return self.name


    @property
    def get_replies(self):
        return self.replies.all() 



class Reply(models.Model):
    post = models.ForeignKey(Comment,on_delete=models.CASCADE,related_name = "replies")
    name = models.CharField(max_length = 200)
    body = models.TextField(default = True)

这是views.py

def BlogDetail(request,pk):

    post = get_object_or_404(Post,pk = pk) 
    comment = CommentForm(request.POST or None)
    subscribe = Subscribe() 
    reply = ReplyForm(request.POST or None)



    if request.method  == 'POST':
        subscribe = Subscribe(request.POST) 
        comment = CommentForm(request.POST)
        reply =  ReplyForm(request.POST)

        if comment.is_valid():
            comment.instance.post = post
            comment.save()  


        elif subscribe.is_valid():
            subscribe = subscribe.save(commit = True)
            return redirect('index')

        elif reply.is_valid():
            reply.instance.com = com
            reply = reply.save(commit = True)
            return redirect('index')

    return render(request,'app/blog.html',{'blog_object':post,'comment':comment,
        'subscribe':subscribe,'reply':reply,
        })

这是blog.html

<form method='POST' action=".">
  {%csrf_token%}
  <div class="row">
    <div class="col-xs-12 col-sm-12 col-md-4">

      <div class="col-inner ts-20 m-sm">
        <input type="submit" value="Subscribe to my daily letter" class="btn btn-primary subscribe">
      </div>
    </div>

    <div class="col-xs-12 col-sm-12 col-md-8">
      <div class="ts-20">
        <div class="form-group form-group-with-icon comment-form-email">

          {{subscribe.email}}
</form>
<div class="form-control-border"></div>


</div>
</div>
</div>
</div>
<h3 style="color: #ff714a; font-weight:300;">
  Leave a comment
</h3>

{% if request.user.is_authenticated %}
<div class="comments">
  <div class="row">
    <div class="container">
    </div>
    <form action="." method="post" id="commentform" class="comment-form">
      {% csrf_token %}
      <div class="col">
        <div class="form-group form-group-with-icon comment-form-email">
          {{comment}}
          <div class="form-control-border"></div>


        </div>
      </div>

      <div class="col">
        <p class="form-submit">
          <input name="submit" type="submit" id="submit" class="submit" value="Post Comment">
        </p>
      </div>
    </form>
  </div>
  {% endif %}

  <div class="post-comments">
    <h3 style="color: #ff714a; font-weight:300;">See the latest comments</h3>
    {% for comment in blog_object.get_comments %}
    <div class="container">
      <div class="row">
        <div class="col comment_head">
          <strong>{{comment.name}}</strong>

          <div class="col comment_body">
            <p>{{comment.body}}</p>
          </div>
        </div>
      </div>

      <div class="border"></div>

    </div>
    <form action="." method='POST'>
      {% csrf_token %} {{reply}}
      <input type="submit" value="submit">
    </form>
    {% endfor %} {% for reply in com.get_replies %}

    <div class="container">
      <div class="row">
        <div class="col comment_head">
          <strong>{{reply.name}}</strong>

          <div class="col comment_body">
            <p>{{reply.body}}</p>
          </div>
        </div>
      </div>
      <div class="border"></div>
      <div class="reply">
      </div>
    </div>


    </form>
    {% endfor %}
  </div>

0 个答案:

没有答案