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