How to automatically select parent comment in reply froms?

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

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

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

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

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?

Thanks KenWhitesell for your reply.
I have an comment froms where all parent comment coming. see the picture


. 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

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.)

KenWhitesell no. It’s not render with “Reply” button. I added this replay froms for add child comment of my parent comment.

<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>

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.

1 Like

got it. But how to add relationship in views?

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.

KenWhitesell 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

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

Review your MPTTModelInstance methods. What method might you have to add an entity as a child of another entity?

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

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

KenWhitesell I aslo tried without MPTT. Still now every comment coming reply froms posting as a parent commnet. see my code:

{% 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  %}

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.

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.

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.