WEB/Django prac

[Django prac][DB N:1 ①] 12. action으로 주소 보내주기, 삭제 버튼 만들기

bay07 2024. 4. 4. 11:32

# articles > templates > articles > detail.html

action으로 주소 보내주는 것도 해주기 

<form action="{% url "articles:delete" article.pk %}" method="POST">

삭제버튼 만들기 

    {% for comment in comments %}
      <li>
        {{ comment.content }}
        <form action="{% url "articles:comments_delete" article.pk comment.pk %}" method="POST" style="display: inline;">
          {% csrf_token %}
          <input type="submit" value="삭제">
        </form>
      </li>
    {% endfor %}

 

더보기
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <h1>Detail</h1>
  <h2>{{ article.pk }} 번째 글</h2>
  <hr>
  <p>제목: {{ article.title }}</p>
  <p>내용: {{ article.content }}</p>
  <p>작성일: {{ article.created_at }}</p>
  <p>수정일: {{ article.updated_at }}</p>
  <hr>
  <a href="{% url "articles:update" article.pk %}">UPDATE</a>
  <form action="{% url "articles:delete" article.pk %}" method="POST">
    {% csrf_token %}
    <input type="submit" value="DELETE">
  </form>
  <hr>
  <h3>댓글 작성</h3>
  <form action="{% url "articles:comments_create" article.pk %}" method="POST">
    {% csrf_token %}
    {{ comment_form }}
    <input type="submit" value="댓글 작성">
  </form>
  <hr>
  <h3>댓글 목록</h3>
  <ul>
    {% for comment in comments %}
      <li>
        {{ comment.content }}
        <form action="{% url "articles:comments_delete" article.pk comment.pk %}" method="POST" style="display: inline;">
          {% csrf_token %}
          <input type="submit" value="삭제">
        </form>
      </li>
    {% endfor %}
  </ul>
  <hr>
  <a href="{% url "articles:index" %}">[back]</a>
</body>
</html>

 

# 서버 열어서 확인해보기 

삭제 버튼이 잘 생겼고, 삭제 기능이 잘 되는지 확인하면 된다. 

save all 

python manage.py runserver