Функция ответа на мой комментарий не работает? - PullRequest
0 голосов
/ 05 марта 2019

Я веду блог, но застрял в функции ответов на комментарии.Я не знаю, как сделать функцию ответа, из которой пользователи могут отвечать на комментарии.Я много пробовал, но он по-прежнему отображает эти комментарии, как и другие комментарии, а не как ответы на этот конкретный комментарий.

Здесь представлен models.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,
        })

Вот блог.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>
...