오늘은 팀 프로젝트(인스타 클론 코딩)의 main 페이지에 있는 게시글의 글 삭제 버튼 기능을 구현했다.
우선 django template의 if문으로 글을 작성한 사용자만 글 삭제 버튼이 활성화 될 수 있도록 작업했다.
{% if tw.nickname == user %}
<a href="/main/user_profile_edit/{{ tw.write_no }}"
style="width: auto; height: 30px; margin-left: auto; margin-right: 0px;">
<button type="button" class="btn btn-outline-secondary"
style="width: auto; height: 32px; diplay: flex; align-items:center;">수정</button>
</a>
<a href="/main/user_profile_delete/{{ tw.write_no }}"
style="width: auto; height: 30px; margin-left: 13px; margin-right: 3px;">
<button type="button" class="btn btn-outline-secondary" onclick="delete_feed()"
style="width: auto; height: 32px; diplay: flex; align-items:center;">삭제</button>
</a>
{% endif %}
그리고 버튼을 눌렀을 때 a태그로 /main/user_profile_delete/{{ tw.write_no }}"의 경로로 이동하여 user_profile_delete 함수가 실행이 된다.
@login_required
def user_profile_delete(request, write_no):
current_user_tweet = TweetModel.objects.get(write_no=write_no)
current_user_tweet.delete()
return redirect('/main')
html에서 request로 write_no를 받아와 views.py에서 함수 실행 후 db에서 해당 글만 삭제 될 수 있도록 처리해주었다.