23.4.17 TIL

HS L·2023년 4월 18일
0

내일배움캠프

목록 보기
32/73

Django 프로젝트 피드백

구현한 기능중 개선사항들 시도

댓글 개수를 세기위해 변수추가보다는 역참조를 사용하기

23.4.18 수정 -> 23.4.18 TIL 정참조 역참조

댓글삭제 유저 유효성 검사 추가하기

# 댓글 삭제
@login_required
def post_comment_delete(request, id):
    post_comment = PostComment.objects.get(id=id)
    post = Post.objects.get(id=post_comment.post.id)
    post_comments = PostComment.objects.filter(post=post)

    # 요청한 유저가 댓글 작성자 or 게시글 작성자와 일치하는 경우 조건 추가
    if request.user == post_comment.author or request.user == post_comment.post.author:
        post_comment.delete()
        post.comment_count = len(post_comments)
        post.save()

    return render(request, 'detailpost/post_detail.html',
                          {'post': post, 'post_comments': post_comments,
                           })
profile
식이

0개의 댓글