django How to review blog comment before publish?

I am new in django. I want to review my blog comment before it publish or show in my html template. I am using MPTTModel in my comment model for child parent relationship in my comment section. I used BooleanField in my models but it’s not working. Right now my html template showing all blog comment when any user submitting comment. How to prevent to show comment in my html template without my approve. here is my code:

#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')
      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)
      is_approve = models.BooleanField(default=False)

#forms.py

class CommentFrom(forms.ModelForm):
      class Meta:
          model = BlogComment
          fields = ['name','email','comment']

views.py

class BlogDetail(DetailView):
      model = Blog
      template_name = 'blog_details.html'      
      
      def get(self,request,slug):
          blog = Blog.objects.get(slug=slug)
          form = CommentFrom()
          context = {'form':form,
                     'blog':blog,
                   }
          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)

#html

{% load mptt_tags %}
                            
{%  recursetree blog.blogcomment_set.all %}
<p>comment: {{node.comment}}</p>
            {% if not node.is_leaf_node %}
               <div class="children pl-2 pl-md-5">
                {{ children }}
                </div>
            {% endif %}
{% endrecursetree %}
                             

This is what’s grabbing all comments.
What you probably want to do is retrieve the comments using a query in the view, filtering the comments as necessary. Then you can pass that queryset into the template in the context, and iterate over the queryset in the template.

1 Like

Thanks KenWhitesell. how to use filtering in my view ? so I can review every comment before it publish . As you said {% recursetree blog.blogcomment_set.all %} it’s garbing all comment then what I will use instead of this.?

How do you perform any query in a view? How do you filter any query?

See: QuerySet API reference | Django documentation | Django and Lookup API reference | Django documentation | Django.

KenWhitesell I need to use recursetree for maintain parent child relationship in my comment selction.

No, you do not need to use that template tag. There is a complete set of methods available on the api for querying that data in your view. See https://django-mptt.readthedocs.io/en/latest/models.html#mpttmodel-instance-methods

KenWhitesell thanks let me check

KenWhitesell Thanks a looot… It’s working now. after using this queryset filter in my view it’s working now :slight_smile::slight_smile: :slight_smile: queryset = BlogComment.objects.filter(is_approve=True) Thanks again for your help

I’d need to see the complete view and the template.

KenWhitesell Thanks a looot… It’s working now. after using this queryset filter in my view it’s working now :slight_smile::slight_smile: :slight_smile: queryset = BlogComment.objects.filter(is_approve=True) Thanks again for your help