简体   繁体   English

我如何能够在 Django 中显示我的评论回复

[英]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意见.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 urls.py 和 models.py 不需要更改,因为这确实有效,并且它显示在管理界面和所有内容中,但是我不知道如何在我的 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 .所以实际的父评论显示没有问题,但随后它会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.我觉得这与in comment.replys有关,但我不知道是什么。

I have tried changing replys to replies , however I got an error, I changed it simply to reply , and it still didn't work.我曾尝试将replys更改为replies ,但出现错误,我将其更改为仅reply ,但仍然无法正常工作。 I then changed it to post.comment.reply , however again, this did not work.然后我将其更改为post.comment.reply ,但同样,这不起作用。 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我取出了田地,因为它们并不重要

The link from comment to reply is called replies , and it is a manager.从评论到回复的链接称为replies ,它是一个经理。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM