THe 'save()' method
- save(commit=False)
- Create, but don't save the new instance.
- 아직 데이터베이스에 저장되지 않은 인스턴스를 반환
- 저장하기 전에 객체에 대한 사용자 지정 처리를 수행할 때 유용하게 사용
@require_POST
def comments_create(request,pk) :
article = Article.objects.get(pk=pk)
comment_form = CommentForm(request.POST)
if comment_form.is_valid() :
comment = comment_form.save(commit=False)
comment.article = article
comment.save()
return redirect('articles:detail',article.pk)
@require_safe
def detail(request, pk):
article = get_object_or_404(Article,pk=pk)
comment_form = CommentForm()
comments = article.comment_set.all()
context = {
'article': article,
'comment_form' : comment_form,
'comments' : comments
}
return render(request, 'articles/detail.html', context)
Commnet DELETE
path('<int:article_pk>/comments/<int:comment_pk>/delete/',views.comments_delete,
name='comments_delete')
def comments_delete(request,article_pk,comment_pk) :
comment = Comment.objects.get(pk=comment_pk)
comment.delete()
return redirect('articles:detail',article_pk)