jeremy_lord
jeremy_lord

Reputation: 469

How am I able to display my comment replies in Django

I have a system where users can make posts, and I have recently made it so users can reply to these comments. However, they won't display. Here is some code. VIEWS.PY

@login_required(login_url='/mainapp/user_login/')
def add_comment_to_post(request,pk):
    post = get_object_or_404(Post,pk=pk)
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.author = request.user # add this line
            comment.save()
            return redirect('mainapp:post_detail',pk=post.pk)

    else:
        form = CommentForm()
    return render(request,'mainapp/comment_form.html',{'form':form})

def get_queryset(self):
    return Comment.objects.filter(created_date__lte=timezone.now()).order_by('-created_date')

@login_required(login_url='/mainapp/user_login/')
def add_reply_to_comment(request,pk):
    comment = get_object_or_404(Comment,pk=pk)
    if request.method == 'POST':
        form = ReplyForm(request.POST)
        if form.is_valid():
            reply = form.save(commit=False)
            reply.comment = comment
            reply.author = request.user
            reply.save()
            return redirect('mainapp:post_detail',pk=comment.pk)
    else:
        form = ReplyForm()
    return render(request,'mainapp/reply_form.html',{'form':form})
def get_queryset(self):
    return Reply.objects.filter(created_date__lte=timezone.now()).order_by('-created_date')

Those are my views for adding a comment to a post and a view to a post. The urls.py and models.py don't need changing, since this does work, and it shows in the admin interface and all, however I don't know how to display it in my HTML

{% for comment in post.comments.all %}

  {% if user.is_authenticated or comment.approved_comment %}
  <p>Posted by: <strong>{{ comment.author }}</strong></p> 
  <p id="comment-text">{{ comment.text|safe|linebreaks }}</p>
  <p>{{ comment.created_date|timesince }} ago</p><br>
{% endif %}

{% for reply in comment.replys %}
      <p>Posted by: <strong>{{ reply.author }}</strong></p> 
      <p id="comment-text">{{ reply.text|safe|linebreaks }}</p>
      <p>{{ reply.created_date|timesince }} ago</p><br>
      {% endfor %}
  {% empty %}
      <p>No comments posted.</p>
  {% endfor %}

So the actual parent comment is getting displayed no problem, but then it gets to the for reply in comment.replys. I don't know what I am doing wrong here. I feel like it has to do with something about the in comment.replys, but I don't know what.

I have tried changing replys to replies, however I got an error, I changed it simply to reply, and it still didn't work. I then changed it to post.comment.reply, however again, this did not work. I have tried heaps and heaps, but nothing is working.

So what I want is for the reply to be displayed underneath the comment, and it should be working, except it isn't; I don't know where I am going wrong, so any help would be good

EDIT: Here are the models

class Comment(models.Model):
    post = models.ForeignKey('mainapp.Post',related_name='comments',on_delete=models.CASCADE)
    #...

    def get_absolute_url(self):
        return reverse('post_list')

    def __str__(self):
        return self.text

class Reply(models.Model):
    comment = models.ForeignKey('mainapp.Comment',related_name='replies',on_delete=models.CASCADE)
    #...

    class Meta:
        verbose_name_plural = "replies"

    def get_absolute_url(self):
        return reverse('post_list')

    def __str__(self):
        return self.text

I took out the fields, since they're not important

Upvotes: 0

Views: 618

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599540

The link from comment to reply is called replies, and it is a manager.

{% for reply in comment.replies.all %}

Upvotes: 2

Related Questions