📢 공지사항
home

Commentapp 마무리

복습
수강일
수강일_
숫자
38
주차
2주차
체크
태그
Commentapp Implementation

Comment 시각화

detail.html에 for문으로 삽입한다.
{% for comment in target_article.comment.all %} {% include 'comment/detail.html' with comment=comment %} {% endfor %}
Python
복사
detail.html도 작성해준다.
<div style="border: 1px solid: text-align: left; padding: 4%; border-radius: 1rem; border-color: #bbb;"> <div> <strong> {{ comment.writer.profile.nickname }} </strong> {{ comment.created_at }}} </div> <div style="margin: 1rem 0;"> {{ comment.content }} </div> </div>
HTML
복사

delete view 작성

class CommentDeleteView(DeleteView): model = Comment context_object_name = 'target_comment' template_name = 'commentapp/delete.html' def get_success_url(self): return reverse('articleapp:detail', kwargs={'pk': self.object.article.pk})
Python
복사
<div style="margin: 1rem 0;"> {{ comment.content }} </div> {% if comment.wirter == request.user %} <div> <a href="{% url 'commentapp:delete' pk=comment.pk %}" class="btn btn-danger rounded-pipp col-6"> Delete </a> </div> {% else %} </div>
HTML
복사
{% extends 'base.html' %} {% block content %} <div style = "text-align: center; max-width: 600px; margin: 4rem auto"> <div class="mb-4"> <h4>Delete Comment : {{ target_comment.content }}</h4> </div> <form action="{% url 'commentapp:delete' pk=target_comment.pk%}" method="post"> {% csrf_token %} {% bootstrap_form form %} <input type="submit" class="btn btn-danger rounded-pill col-6 mt-3"> </form> </div> {% endblock %}
HTML
복사
마지막으로, delete view에 decorator까지 넣어주면 된다.
from django.http import HttpResponseForbidden from commentapp.models import Comment def comment_ownership_required(func): def decorated(request, *args, **kwargs): comment = Comment.objects.get(pk=kwargs['pk']) if not comment.writer == request.user: return HttpResponseForbidden() return func(request, *args, **kwargs) return decorated
Python
복사