# How to automatically select parent comment in reply froms?

**URL:** <https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244>\
**Category:** Using Django\
**Created:** [June 7, 2021, 10:56am UTC](https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244 "2021-06-07T10:56:28Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![MDFARHYN](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mdfarhyn/32/4519_2.png) [@MDFARHYN](https://forum.djangoproject.com/u/MDFARHYN)\
**Post date:** [June 7, 2021, 10:56am UTC](https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244/1 "2021-06-07T10:56:28Z")

</div>

Hello I am new in django. I created an comment system where user can post comment. Now I want to add reply to my comment. Basically I want to automatically select parent comment in my reply froms. here is my code:

**views.py**

```auto
class BlogDetail(DetailView):
      model = Blog
      template_name = 'blog_details.html'      
      
      def get(self,request,slug):
         
          blog = Blog.objects.get(slug=slug)
          queryset = BlogComment.objects.filter(is_published="0",blog=blog)
          
          form = CommentFrom()
          context = {'form':form,
                     'blog':blog,
                     'queryset':queryset
                   }
          return render(request,'blog_details.html',context)

      def post(self,request,slug):
          blog = Blog.objects.get(slug=slug)
          form = CommentFrom(request.POST)
              
          if form.is_valid():
             comment = form.save(commit=False) 
             comment.blog = blog
             comment.save()
             messages.add_message(self.request, messages.INFO, 'Your Comment pending for admin approval')
             return redirect(reverse('blog-detail', kwargs={'slug':slug}))
          
          else:
               form()
          
          

          context = {'form':form,
                     'blog':blog,
                                       
                   }

          return render(request,'blog_details.html',context)

```

**#models.py**

```auto
class BlogComment(MPTTModel):
      blog = models.ForeignKey(Blog,on_delete=models.CASCADE)
      parent = TreeForeignKey('self', on_delete=models.CASCADE,
                            null=True, blank=True, related_name='children')
      sno = models.AutoField(primary_key=True)                      
      author = models.ForeignKey(User,on_delete=models.CASCADE,blank=True,null=True)                      
      name = models.CharField(max_length=250)
      email = models.EmailField(max_length=2000)
      comment = models.TextField(max_length=50000)
      created_at = models.DateTimeField(auto_now_add=True,blank=True,null=True)
      updated_at = models.DateTimeField(auto_now=True,blank=True,null=True)
   
      CHOICES = (
        ('0', 'published',),
        ('1', 'pending',),
        ('2', 'rejected',),
        )
      is_published = models.CharField(
        max_length=1,
        choices=CHOICES,default='1'
      )

```

right now I can only add reply form my django admin panel by selecting parent comment. I want to add reply through my html reply froms. somethings like this: [![enter image description here](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/4/484a745ad3c49014563e7507dc15a170a55d3f8f.png)](https://i.stack.imgur.com/VTW6J.png)

**How to automatically select parent comment in my html reply froms?**

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 7, 2021, 12:20pm UTC](https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244/2 "2021-06-07T12:20:30Z")

</div>

Does every comment render with a “Reply” button under it? If so, what does that button do?  
If not, is it just one “Reply” button under all comments?

---

<div class="post-metadata">

**Author:** ![MDFARHYN](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mdfarhyn/32/4519_2.png) [@MDFARHYN](https://forum.djangoproject.com/u/MDFARHYN)\
**Post date:** [June 7, 2021, 12:32pm UTC](https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244/3 "2021-06-07T12:32:40Z")

</div>

> [@KenWhitesell](#):
>
> button under all comments?

Thanks [KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell) for your reply.  
I have an comment froms where all parent comment coming. see the picture

 ![Capture](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/2/2c9ea6f82b3a2dea8f52af56a924f8edc2c63802.png)  
. I want to add child comment though reply froms. such as Comment1 is a parent comment. If I add commnet2 through my comment forms it will be parent comment2. But I want commnet2 will be child of comment1

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 7, 2021, 12:45pm UTC](https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244/4 "2021-06-07T12:45:44Z")

</div>

Unfortunately, your explanation doesn’t answer my questions.

Does **every** comment render with a “Reply” button under it?

If so, what does that button do? (Please be detailed and precise. Include the html and/or javascript if necessary.)

---

<div class="post-metadata">

**Author:** ![MDFARHYN](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mdfarhyn/32/4519_2.png) [@MDFARHYN](https://forum.djangoproject.com/u/MDFARHYN)\
**Post date:** [June 7, 2021, 1:11pm UTC](https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244/5 "2021-06-07T13:11:46Z")

</div>

[KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell) no. It’s not render with “Reply” button. I added this replay froms for add child comment of my parent comment.

```auto
<h5 class="card-header">Post a Replay:</h5>
  <div class="card-body">
<form method="POST">

           {% csrf_token %}

                 <div class="row">

                   <div class="col">

       <input type="text" name="name" class="form-control" placeholder="Your Name" required>

    </div>

<div class="col">

                                                                               

                                                                                <input type="email" name="email" class="form-control" placeholder="Enter email" required>

   </div>

  </div>

 <label for="exampleFormControlTextarea1">Comment</label>

                                                                      

  <textarea class="form-control" name="comment" id="exampleFormControlTextarea1" rows="3"></textarea>

   <button class="btn btn-primary" type="submit">Submit</button>

  </form>

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 7, 2021, 1:17pm UTC](https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244/6 "2021-06-07T13:17:38Z")

</div>

You’ve got a couple different options, but the easiest is probably to render a hidden input field containing the PK of the parent comment, and use that to set the relationship in the view that processes the form.

---

<div class="post-metadata">

**Author:** ![MDFARHYN](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mdfarhyn/32/4519_2.png) [@MDFARHYN](https://forum.djangoproject.com/u/MDFARHYN)\
**Post date:** [June 7, 2021, 1:19pm UTC](https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244/7 "2021-06-07T13:19:50Z")

</div>

got it. But how to add relationship in views?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 7, 2021, 1:22pm UTC](https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244/8 "2021-06-07T13:22:02Z")

</div>

Set the ForeignKey field to the value submitted in that hidden input field. You can either do it in the form itself, or in your `post` function in your view.

---

<div class="post-metadata">

**Author:** ![MDFARHYN](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mdfarhyn/32/4519_2.png) [@MDFARHYN](https://forum.djangoproject.com/u/MDFARHYN)\
**Post date:** [June 7, 2021, 1:58pm UTC](https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244/9 "2021-06-07T13:58:57Z")

</div>

[KenWhitesell](https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244/8) I passed my parent foreign key ` <input type="hidden" name="parent_id" value="{{q.parent}}">` but still now it’s posting as a parent commnet.

#models.py

```auto
 parent = TreeForeignKey('self', on_delete=models.CASCADE,
                            null=True, blank=True, related_name='children')

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 7, 2021, 2:02pm UTC](https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244/10 "2021-06-07T14:02:52Z")

</div>

Review your [MPTTModelInstance methods](https://django-mptt.readthedocs.io/en/latest/models.html#mpttmodel-instance-methods). What method might you have to add an entity as a child of another entity?

---

<div class="post-metadata">

**Author:** ![MDFARHYN](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mdfarhyn/32/4519_2.png) [@MDFARHYN](https://forum.djangoproject.com/u/MDFARHYN)\
**Post date:** [June 7, 2021, 2:05pm UTC](https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244/11 "2021-06-07T14:05:24Z")

</div>

it will be node` input type="hidden" name="parent_id" value="{{node.parent}}">` but still now commnet post form reply froms as posting as a parent comment

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 7, 2021, 2:13pm UTC](https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244/12 "2021-06-07T14:13:15Z")

</div>

A `node` is an object. What **method** in a `node` object allows you to specify that a node is a child of another node?

---

<div class="post-metadata">

**Author:** ![MDFARHYN](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mdfarhyn/32/4519_2.png) [@MDFARHYN](https://forum.djangoproject.com/u/MDFARHYN)\
**Post date:** [June 7, 2021, 2:19pm UTC](https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244/13 "2021-06-07T14:19:10Z")

</div>

[KenWhitesell](https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244/8) I aslo tried without MPTT. Still now every comment coming reply froms posting as a parent commnet. see my code:

```auto
{% for q in queryset %} #I am using queryset for review every commnet before it showing in my html template.
name: {{q.name}}
comment:{{q.commnet}}
replay to this commnet:
 {% csrf_token %}
    
 <input type="hidden" name="parent_id" value="{{q.parent}}"> #as you said. I passing parent value as hidden input.
<input type="text" name="name" class="form-control" placeholder="Your Name" required>
                                                                                
<input type="email" name="email" class="form-control" placeholder="Enter email" required>
 <textarea class="form-control" name="comment" id="exampleFormControlTextarea1" rows="3">
</textarea>
 <button class="btn btn-primary" type="submit">Submit</button>
</form> 
{% endfor %}

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 7, 2021, 6:49pm UTC](https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244/14 "2021-06-07T18:49:18Z")

</div>

The issue you’re facing here has very little to do with the form itself, and more with how you’re trying to work with the MPTT data structures.

You need to identify what method you use in the MPTT library that allows you to assign a node as a child node of another node, and use that information to assign the new comment as a child node of the parent comment.

---

<div class="post-metadata">

**Author:** ![MDFARHYN](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mdfarhyn/32/4519_2.png) [@MDFARHYN](https://forum.djangoproject.com/u/MDFARHYN)\
**Post date:** [June 7, 2021, 7:31pm UTC](https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244/15 "2021-06-07T19:31:12Z")

</div>

> [@KenWhitesell](#):
>
> n your `post` function in your view.

Thanks for your reply. Now I am not using MPTT in my models. I am using models.model and still now I am facing the issue. All reply posting as a parent comment.

---

<div class="post-metadata">

**Author:** ![MDFARHYN](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mdfarhyn/32/4519_2.png) [@MDFARHYN](https://forum.djangoproject.com/u/MDFARHYN)\
**Post date:** [June 7, 2021, 8:11pm UTC](https://forum.djangoproject.com/t/how-to-automatically-select-parent-comment-in-reply-froms/8244/16 "2021-06-07T20:11:23Z")

</div>

[KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell) Thanks. I was missing this` <input type="hidden" name="parent" id="parent_id" value="{{node.sno}}">` . After lot of hours struggling I find out the solution. As you mentioned I need to be add an hidden fields by passing values and it works :). I create an AutoField fields in my models which name sno. then pass the sno value in my hidden input fields and it works.
