i tried this way:
class PostDetail(DetailView):
model=Post
template_name='Post_detail.html'
form_class=CommentForm
def get_success_url(self):
return reverse ('post_detail',kwargs={'pk':self.object.pk})
def get_context_data(self, **kwargs):
context = super(PostDetail, self).get_context_data(**kwargs)
context['comment_form']=CommentForm(initial={'post':self.object})
context['comments']=self.object.comments.filter(approved=True)
return context
def post(self,request):
self.object=self.get_object()
comment_form=self.get_form()
if comment_form.is_valid():
return self.comment_form_valid(comment_form)
else:
return self.comment_form_valid(comment_form)
def comment_form_valid(self,comment_form):
comment_form.save()
return super(PostDetail,self).comment_form_valid(comment_form)
now the error is :
'Post' object has no attribute 'comments'
but in the models.py:
class Comment(models.Model):
post = models.ForeignKey(Post,on_delete=models.CASCADE,related_name='comments')
name = models.CharField(max_length=80)
body = models.TextField()
reply=models.ForeignKey('Comment',on_delete=models.CASCADE,null=True)
def __str__(self):
return self.name
so what is the remedy?
'