| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- poetry
- sql
- github
- WebSocket
- REDIS
- re-id
- vscode
- Commpot
- channels
- 마스킹
- 1주차
- 채팅
- Class
- 정보처리기사
- 가상환경
- 장고
- WHERE절
- 파이썬
- js
- 프로그래머스
- 개발일지
- resnet50
- Git
- 알고리즘
- 미니프로젝트
- 2주차
- 프로젝트
- WIL
- 백준
- 정보처리기사실기
Archives
- Today
- Total
개발일기
게시글 자세히보기/삭제하기/댓글 작성하기/삭제하기 기능 본문
오늘 할 일
게시글 상세 페이지 기능과 template 기본 골격 만들기
게시글 상세 페이지에서의 기능들 - 삭제하기, 댓글 작성하기, 댓글 삭제하기 구현하기
1. 게시글 자세히보기
게시글을 자세히 볼 때,
게시글 안에 나타나야하는 데이터들: 게시글(제목, 내용), 작성자, 작성일과 수정일
-> Posting 모델에 담겨있는 정보를 다 가지고 와서 보여주면 된다..! -> Get method를 사용하면 된다.
def posting_detail_view(request, id):
my_posting = Posting.objects.get(id=id)
# 게시글 id를 이용해 그 게시글의 정보를 가지고 오자
posting_comment = PostingComment.objects.filter(posting_id = id)
# 이것은 댓글을 보여주기 위한 코드로, 이에 대한 자세한 설명은 아래에 하겠다.
return render(request,'posting/post_detail.html',{'posting':my_posting,'comment':posting_comment})
게시글 자세히보기 templates는 다음과 같다.
templates는, post_detail.html을 만들어서 홈 화면에서 게시글 보기 버튼을 누르면, 상세페이지로 이동하도록 했다.
그리고 그 상세페이지에는 게시글 수정, 삭제, 댓글 작성과 수정, 삭제가 가능하도록 했다.
(댓글 작성, 수정, 삭제는 아래 댓글 작성, 삭제 부분 template을 확인하기 :))
# templates/posting/post_detail.html
<!--skip-->
<div class="media">
<div class="media-body">
<h4 class="card-title">{{ posting.title }}</h4>
<div style="text-align: right">
<p class="card-text">
<h5 class="card-title">{{posting.author}}</h5>
<h5 style="font-size: small">{{ posting.created_at }}</h5>
<h5 style="font-size: small">{{ posting.updated_at|timesince }}전</h5>
</p>
{% if posting.author == user %}
<a href="/api/posts/edit/{{posting.id}}"> # 게시글 수정 url
<span class="badge rounded-pill bg-danger">수정</span>
</a>
<a href="/api/posts/delete/{{posting.id}}"> # 게시글 삭제 url
<span class="badge rounded-pill bg-danger">삭제</span>
</a>
{% endif %}
</div>
<div class="media">
<div class="media-bod">
<h5 class="mt-0">{{ posting.content }}</h5>
</div>
</div>
</div>
</div>
<!--skip-->
2. 게시글 삭제하기
@login_required
def delete_posting(request, id):
my_post = Posting.objects.get(id=id)
my_post.delete()
return redirect('/post')
# return HttpResponse('글 삭제 완료')
delete() 를 이용하면 끝~ 생각보다 간단하다~ 장고의 매력 :)
3. 댓글 작성하기/ 삭제하기
def write_comment_view(request, id: int) -> HttpResponse:
if request.method == 'POST':
comment = request.POST.get("comment", "")
current_posting = Posting.objects.get(id=id)
user = request.user.is_authenticated
if not user:
return redirect('/api/user/login')
PC = PostingComment()
PC.comment = comment
PC.author = request.user
PC.author_name = request.user.username
PC.posting = current_posting
PC.save()
return redirect('/api/posts/'+str(id))
@login_required
def delete_comment_view(request, id):
comment = PostingComment.objects.get(id=id)
current_posting = comment.posting.id
if comment.author == request.user or request.user.is_superuser:
comment.delete()
return redirect('/api/posts/'+str(current_posting))
댓글 삭제는 댓글을 작성한 사람만이 가능하도록 하기 위해서
if 문을 이용해 comment.author == request.user 일 때만 가능하도록 코드를 구성했다.
이때.. 관리자도 삭제가 가능하도록 request.user.is_superuser도 추가해주었다.
<!-- 댓글 작성 하는 곳 -->
<form class="input-group mb-3" action="/api/posts/comment/{{posting.id}}" method="post">
{% csrf_token %}
<input type="text" class="form-control" id='comment' name='comment' placeholder="댓글을 작성해주세요" />
<button class="btn btn-outline-secondary" type="submit">작성</button>
</form>
반복문을 사용해서 작성된 모든 댓글을 볼 수 있도록 하자!
<!-- 반복문이 들어 갈 곳 -->
{% for cm in comment %}
<div class="row">
<div class="col-md-12">
<div class="media">
<div class="media-body">
<h5 class="mt-0"> {{ cm.comment }} </h5>
</div>
<div style="float: right">
<span style="font-size: small">
{{ cm.author }} - {{ cm.created_at | timesince }}전
</span>
</div>
<br>
<div style=" float: right">
{% if cm.author == user %}
<a href="/api/posts/comment/modify/{{ cm.id }}">
<span class="badge bg-danger">수정</span>
</a>
{% endif %}
{% if cm.author == user or user.is_superuser %}
<a href='/api/posts/comment/delete/{{ cm.id }}'>
<span class="badge bg-danger">삭제</span>
</a>
{% endif %}
</div>
</div>
</div>
</div>
<hr>
{% endfor %}
<!-- 반복문이 끝난 곳 -->
'Project Portfolio' 카테고리의 다른 글
| 페이징(pagination) 구현하기 (0) | 2023.04.14 |
|---|---|
| 게시글 수정하기 기능(모달과 페이지 두 가지 버전으로 만들기) (0) | 2023.04.13 |
| 장고 팀프로젝트 #1 Commpot 기획 (0) | 2023.04.11 |
| 좀비 턴제 게임 만들기 #3 1대 다수 플레이 구현하기 (0) | 2023.04.02 |
| 좀비 턴제 게임 만들기 #2 제거할 캐릭터 선택하기(리스트 활용) (0) | 2023.03.30 |