How to make add replies to comments in Django?

12,023

Solution 1

I had the same problem and resolved it as follows:

1. For admin site as mentioned above just set blank=True for parent field. My comment model:

class Comment(models.Model):
    post = models.ForeignKey(Post, related_name='comments')
    name = models.CharField(max_length=80)
    email = models.EmailField(max_length=200, blank=True)
    body = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    # manually deactivate inappropriate comments from admin site
    active = models.BooleanField(default=True)
    parent = models.ForeignKey('self', null=True, blank=True, related_name='replies')

    class Meta:
        # sort comments in chronological order by default
        ordering = ('created',)

    def __str__(self):
        return 'Comment by {}'.format(self.name)
  • remember to run makemigrations and migrate

2.Let's start with views. I'm using the post_detail view to display the post and its comments. We add a QuerySet to retrieve all parent active comments for this post. After this, we validate the submitted data using the form's is_valid(). If the form is valid we check if submitted data comes from hidden input in replay button form. Next if parent_id exits we create parent object(parent_obj) for replay comment and replay_comment object, then we assign parent_obj to replay_comment. If parent_obj is equal to None we just proceed with normal comment by creating new_comment object and saving it to the database.

def post_detail(request, post):
    # get post object
    post = get_object_or_404(Post, slug=post)
    # list of active parent comments
    comments = post.comments.filter(active=True, parent__isnull=True)
    if request.method == 'POST':
        # comment has been added
        comment_form = CommentForm(data=request.POST)
        if comment_form.is_valid():
            parent_obj = None
            # get parent comment id from hidden input
            try:
                # id integer e.g. 15
                parent_id = int(request.POST.get('parent_id'))
            except:
                parent_id = None
            # if parent_id has been submitted get parent_obj id
            if parent_id:
                parent_obj = Comment.objects.get(id=parent_id)
                # if parent object exist
                if parent_obj:
                    # create replay comment object
                    replay_comment = comment_form.save(commit=False)
                    # assign parent_obj to replay comment
                    replay_comment.parent = parent_obj
            # normal comment
            # create comment object but do not save to database
            new_comment = comment_form.save(commit=False)
            # assign ship to the comment
            new_comment.post = post
            # save
            new_comment.save()
            return HttpResponseRedirect(post.get_absolute_url())
    else:
        comment_form = CommentForm()
    return render(request,
                  'core/detail.html',
                  {'post': post,
                   'comments': comments,
                   'comment_form': comment_form})

Simple comment form:

class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ('name', 'email', 'body')

* More about ModelForm

And lastly template. We need to create two forms. One form for comments and the second one for replays. Here you have simple template:

<!-- Comments Form --> 
<h2>Add a new comment</h2>
<form action="." method="post">
    {{ comment_form.as_p }}
    {% csrf_token %}
    <button type="submit">Add comment</button>
</form>

<!-- Comment with nested comments -->
{% for comment in comments %}
    <div class="comment" style="background-color: powderblue">
        <p class="info">{{ comment.name }} | {{ comment.created }}</p>
            {{ comment.body|linebreaks }}

        {% for replay in comment.replies.all %}
            <p class="info">{{ replay.name }} | {{ replay.created }}</p>
            <li>{{ replay.body }}</li>
        {% endfor %}

        <h5>Replay</h5>
        <form action="." method="post">
            {{ comment_form.as_p }}
            {% csrf_token %}
            <!-- Hidden input for parent comment.id -->
            <input type="hidden" name="parent_id" value="{{ comment.id }}">
            <input class="btn btn-primary" type="submit" value="Replay">
        </form>
    </div>
{% empty %}
<h4>There are no comments yet.</h4>
{% endfor %}

just add some nice css and maybe jquery to have fade in reply comments and that's all.

Solution 2

first Question:parent must be set in admin.

parent = models.ForeignKey('self', null=True, blank=True, related_name='replies')

blank=True can let you don't set parent in admin.

second Question:add comment dynamicly.

<form id="comment-form" method="post" role="form">
    {% csrf_token %}
    <textarea id="comment" name="comment" class="form-control" rows="4" placeholder="input comment!"></textarea>
    <button type="submit" class="btn btn-raised btn-primary pull-right">submit</button>
</form>

    $('#comment-form').submit(function(){
    $.ajax({
        type:"POST",
        url:"{% url 'article_comments' article.en_title %}",
        data:{"comment":$("#comment").val()},
        beforeSend:function(xhr){
            xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken'));  
        },
        success:function(data,textStatus){
            $("#comment").val("");
            $(".comment ul").prepend(data);
        },
        error:function(XMLHttpRequest, textStatus, errorThrown){
            alert(XMLHttpRequest.responseText);

        }

    });
    return false;
});

view.py:

    print_comment = u"<p>comment:{}</p>".format(text)
    if parent:
        print_comment = u"<div class=\"comment-quote\">\
                              <p>\
                                  <a>@{}</a>\
                                  {}\
                              </p>\
                          </div>".format(
                              parent.user.username,
                              parent.text
                          ) + print_comment
    # current comment
    html = u"<li>\
                <div class=\"comment-tx\">\
                    <img src={} width=\"40\"></img>\
                </div>\
                <div class=\"comment-content\">\
                    <a><h1>{}</h1></a>\
                    {}\
                    <p>{}</p>\
                </div>\
            </li>".format(
                img,
                comment.user.username,
                print_comment,
                datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            )

    return HttpResponse(html)

Solution 3

models.py

class Comment(models.Model):
    author = models.CharField(max_length=100)
    comment_field = models.TextField()
    date_created = models.DateTimeField(auto_now_add=True)
    post = models.ForeignKey('Post', on_delete=models.CASCADE)
    reply = models.ForeignKey('Comment', on_delete=models.CASCADE, related_name="replies", null=True)

    def __str__(self):
        return self.author

views.py

def post_detail(request, slug):
    post = Post.objects.get(slug=slug)
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            reply_obj = None
            try:
                reply_id = int(request.POST.get('reply_id'))
            except:
                reply_id = None
            if reply_id:
                reply_obj = Comment.objects.get(id=reply_id)

            author = form.cleaned_data['author']
            comment = form.cleaned_data['comment']
            if reply_obj:
               Comment(author=author,comment_field=comment, reply=reply_obj, post=post).save()
            else:
                Comment(author=author,comment_field=comment, post=post).save()
            return redirect(reverse('post_detail', args=[post.slug]))
    else:
        form = CommentForm()
    comments = Comment.objects.filter(post=post, reply=None).order_by('-date_created')
    context = {
        'post':post,
        'form':form,
        'comments':comments
    }
    return render(request, 'post_detail.html', context)

template (post_detail.html)

{% for comment in comments %}
    {{comment.author}}
    {{comment.date_created.date}}
    {{comment.comment_field}}
    
        {% for reply in comment.replies.all %}
            {{reply.author}}
            {{reply.date_created.date}}
            {{reply.comment_field}}
        {% endfor %}
        <a class="text-decoration-none" data-bs-toggle="collapse" href="#collapseExample{{comment.id}}" role="button" aria-expanded="false" aria-controls="collapseExample">
            Reply </a>
        <div class="collapse" id="collapseExample{{comment.id}}">
            <div>
                <form action="" method="POST" enctype="multipart/form-data">
                    {% csrf_token %}
                            <div class="form-group">
                                {{ form.author }}
                            </div>
                            <div class="form-group mt-md-2">
                                {{form.comment}}
                            </div>
                            <input type="hidden" name="reply_id" value="{{ comment.id }}">
                            <button class="btn btn-primary mt-md-1" type="submit" value="Reply">Reply</button>              </form>
            </div>
        </div>
    <hr>
{% endfor %}
Share:
12,023
Deivbid
Author by

Deivbid

Updated on June 04, 2022

Comments

  • Deivbid
    Deivbid almost 2 years

    I'm making my own blog with Django and I already made a Comments system.. I want to add the replies for each comment (like a normal comment's box) and I don't know what to do this is my current models.py comments:

    class Comment(models.Model):
        post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
        author = models.ForeignKey(User, on_delete=models.CASCADE)
        text = models.TextField()
        created_date = models.DateField(auto_now_add=True)
        parent = models.ForeignKey('self', null=True, related_name='replies')
    
        def __str__(self):
            return self.text
    

    and this is the .html where I use the comments

      {% for comment in post.comments.all %}
     <ul>
      {{ comment.text }}
      {% for reply in comment.replies.all %}
          <li>
              {{ reply.text }}
          </li>
      {% endfor %}
     <ul>
     {% endfor %}
    

    and apparently It is working but when I try to make a comment in the admin site of Django it forces me to put a "Parent" to each comment (and this is not obligatory beacuse not every comment is a reply) I also don't know how to add the reply "button" in the HTML file. Please help tell me what changes can I do to make a simple comment box with replies . Thanks a lot