[英]django How to review blog comment before publish?
I am new in django.我是 django 的新手。 I want to review my blog comment before it publish or show in my html template.
我想在我的博客评论发布或显示在我的 html 模板之前查看它。 I am using MPTTModel in my comment model for child parent relationship in my comment section.
我在我的评论部分使用 MPTTModel model 作为子父关系。 I used BooleanField in my models but it's not working.
我在模型中使用了 BooleanField,但它不起作用。 Right now my html template showing all blog comment when any user submitting comment.
现在我的 html 模板在任何用户提交评论时显示所有博客评论。 here is my code:
这是我的代码:
#models.py #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 #forms.py
class CommentFrom(forms.ModelForm):
class Meta:
model = BlogComment
fields = ['name','email','comment']
views.py视图.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 #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 %}
I also tried this query in my views then pass it to context but not seeing any approved comment in my template.我还在我的视图中尝试了此查询,然后将其传递给上下文,但在我的模板中没有看到任何批准的评论。
queryset = BlogComment.objects.filter(blog=blog,is_approve=True)
what I am missing??? queryset = BlogComment.objects.filter(blog=blog,is_approve=True)
我错过了什么???
I was put the queryset = BlogComment.objects.filter(blog=blog,is_approve=True)
in wrong place.我将
queryset = BlogComment.objects.filter(blog=blog,is_approve=True)
放在错误的位置。 after add this queryset inside get method my problem was solved.在 get 方法中添加这个查询集后,我的问题就解决了。 here is code hope it will help others:
这是代码,希望对其他人有所帮助:
def get(self,request,slug):
blog = Blog.objects.get(slug=slug)
queryset = BlogComment.objects.filter(blog=blog,is_approve=True)
form = CommentFrom()
context = {'form':form,
'blog':blog,
'queryset':queryset,
}
return render(request,'blog_details.html',context)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.