๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
๐“๐จ๐๐š๐ฒ ๐ˆ ๐‹๐ž๐š๐ซ๐ง

๐“๐จ๐๐š๐ฒ ๐ˆ ๐‹๐ž๐š๐ซ๐ง 2022.04.13.์ˆ˜

by DevIseo 2022. 4. 13.

TIL : Today I Learn 220413


์˜ค๋Š˜์€ django ๊ฒŒ์‹œํŒ์— ๋Œ“๊ธ€์„ ์ƒ์„ฑํ•˜๊ณ  ์‚ญ์ œํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•ด ํ•™์Šตํ•˜์˜€๋‹ค. DB์ค‘ 1:N ๊ด€๊ณ„์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ–ˆ๋Š”๋ฐ ์˜ค๋Š˜๋„ ์–ด๊น€์—†์ด ์˜ค๋ฅ˜๋ฅผ ๋งŒ๋‚ฌ๋‹ค... ์˜ค๋Š˜์€ ํŽ˜์ด์ง€๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค๋Š” ์˜ค๋ฅ˜๋ฅผ ๋งŒ๋‚˜๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ, ๊ต์ˆ˜๋‹˜์„ ๋”ฐ๋ผํ•˜๋ฉด์„œ comments ๋ถ€๋ถ„์„ comment๋ผ๊ณ  ๋ฐ”๊พธ๋ฉด์„œ ํ•˜๋‚˜์”ฉ ๊ผฌ์—ฌ ๋Œ“๊ธ€์ด ์ƒ์„ฑ๋˜์ง€ ์•Š์•˜์—ˆ๋‹ค. comment๋ผ๊ณ  ๋ฐ”๊พธ๋Š” ๊ฒƒ์€ articles/views.py์—์„œ ์‹œ์ž‘๋˜์—ˆ๋Š”๋ฐ ๋‹ค์‹œ comments๋ผ๊ณ  ์“ฐ๋ฉด์„œ ๊ณ ์ณค๋”๋‹ˆ ํ•ด๊ฒฐ๋˜์—ˆ๋‹ค...

 

@require_POST
def comments_create(request, pk):
    if request.user.is_authenticated:
        article = get_object_or_404(Article, pk=pk)
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():
            comment = comment_form.save(commit=False)
            comment.article = article
            comment.user = request.user
            comment.save()
        return redirect('articles:detail', article.pk)
    return redirect('accounts:login')

@require_POST
def comments_delete(request, article_pk, comment_pk):
    if request.user.is_authenticated:
        comment = get_object_or_404(Comment, pk=comment_pk)
        if request.user == comment.user:
            comment.delete()
    return redirect('articles:detail', article_pk)

๊ทธ๋Ÿฌ๋ฉด์„œ detail.html๊นŒ์ง€ ๊ผฌ์ด๊ฒŒ ๋˜์—ˆ๊ณ  ๋™๊ธฐ์˜ ๋„์›€์„ ๋ฐ›์•„ ๋‹ค์‹œ ์˜ค๋ฅ˜๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

{% extends 'base.html' %}

{% block content %}
  <h1>DETAIL</h1>
  <h3>{{ article.pk }}๋ฒˆ์งธ ๊ธ€</h3>
  <hr>
  <p>์ œ๋ชฉ : {{ article.title }}</p>
  <p>๋‚ด์šฉ : {{ article.content }}</p>
  <p>์ž‘์„ฑ ์‹œ๊ฐ : {{ article.created_at }}</p>
  <p>์ˆ˜์ • ์‹œ๊ฐ : {{ article.updated_at }}</p>
  <hr>
  {% if request.user == article.user %}
    <a href="{% url 'articles:update' article.pk %}">์ˆ˜์ •</a>
    <form action="{% url 'articles:delete' article.pk %}" method="POST">
      {% csrf_token %}
      <input type="submit" value="์‚ญ์ œ">
    </form>
  {% endif %}
  <a href="{% url 'articles:index' %}">back</a>
  <hr>
  <h4>๋Œ“๊ธ€ ๋ชฉ๋ก</h4>
  {% if comments %}
    <p><b>{{ comments|length }}๊ฐœ์˜ ๋Œ“๊ธ€์ด ์žˆ์Šต๋‹ˆ๋‹ค.</b></p>
  {% endif %}
  <ul>
    {% for comment in comments %}
    <li class='mb-3'>{{ comment.user }} : {{ comment.content }}
      {% if request.user == comment.user %}
      <form action="{% url 'articles:comments_delete' article.pk comment.pk %}" method='POST' class='d-inline'>
        {% csrf_token %}
        <input type="submit" value='DELETE' class='btn btn-danger'>
      </form>
      {% endif %}
    </li>
    {% empty %}
      <p>๋Œ“๊ธ€ ์—†์Œ</p>
    {% endfor %}
  </ul>
  <hr>
  {% if request.user.is_authenticated %}
    <form action="{% url 'articles:comments_create' article.pk %}" method='POST'>
      {% csrf_token %}
      {{ comment_form }}
      <input type="submit" value='๋Œ“๊ธ€ ์ž‘์„ฑ' class='btn btn-primary'>
    </form>
  {% else %}
    <a href="{% url 'accounts:login' %}">๋Œ“๊ธ€์„ ์ž‘์„ฑํ•˜๋ ค๋ฉด ๋กœ๊ทธ์ธํ•˜์„ธ์š”</a>
  {% endif %}
{% endblock content %}

๋˜ํ•œ ๊ฒฝ๋กœ์„ค์ •๋„ ๋‹ค์‹œ ํ•ด์ฃผ์–ด์„œ articles/urls.py๋„

from django.urls import path
from . import views


app_name = 'articles'
urlpatterns = [
    path('', views.index, name='index'),
    path('create/', views.create, name='create'),
    path('<int:pk>/', views.detail, name='detail'),
    path('<int:pk>/delete/', views.delete, name='delete'),
    path('<int:pk>/update/', views.update, name='update'),
    path('<int:pk>/comments/',views.comments_create,name='comments_create'),
    path('<int:article_pk>/comments/<int:comment_pk>/delete/',views.comments_delete,name='comments_delete'),
]

๋‹ค์Œ๊ณผ ๊ฐ™์ด views.ํ•จ์ˆ˜ ๋ถ€๋ถ„๊ณผ name=๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•˜์˜€๋‹ค.

+)tmi

์˜ค๋Š˜ ๋ฐฐ์šด ๋ถ€๋ถ„์ด ์ข€ ์–ด๋ ค์›Œ์„œ ์ „์ฒด์ ์œผ๋กœ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•˜๊ณ , DB์— ๋Œ€ํ•œ ๊ฐœ๋…๋„ ๋‹ค์‹œ ํ•œ ๋ฒˆ ๋” ๋ด์•ผ๊ฒ ๋‹ค. ๋‹ค์Œ์ฃผ์— ์ •์ฒ˜๊ธฐ๋„ ๋ด์•ผํ•˜๊ณ  ์ž๋ฐ” ์Šคํ„ฐ๋””๋„ ๋“ค์–ด๊ฐ€์„œ ํ•  ์ผ์ด ์ •๋ง ๋งŽ์ง€๋งŒ ์ด์ œ๋Š” ์ง„์งœ ์ž ์„ ์ค„์—ฌ์„œ๋ผ๋„ ๊ณต๋ถ€ํ•ด์•ผํ•˜๋Š” ์‹œ์ ์ด ์˜จ๊ฑฐ๊ฐ™๋‹ค. ์–ด์ œ ๋ชจ์˜ํ…Œ์ŠคํŠธ๋ฅผ ๋ณด๊ณ  ์—„์ฒญ๋‚œ ์ ˆ๋ง๊ฐ์„ ๋А๊ผˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ์ข‹์€ ์‚ฌ๋žŒ๋“ค์˜ ์‘์› ๋•๋ถ„์— ๊ธˆ๋ฐฉ ๊ทน๋ณตํ–ˆ๋‹ค! ์•Œ๊ณ ๋ฆฌ์ฆ˜๋„ ์–ด๋ ต๋‹ค๊ณ  ํฌ๊ธฐํ•˜์ง€๋ง๊ณ  ๊พธ์ค€ํ•˜๊ฒŒ ๊ณต๋ถ€ํ•ด์„œ ์ทจ์—…์— ๊ผญ ์„ฑ๊ณตํ•ด์•ผ๊ฒ ๋‹ค... ํ˜„์‹ค์— ์•ˆ์ฃผํ•˜์ง€ ๋ง๊ณ  ์•ž์œผ๋กœ ๋‚˜์•„๊ฐ€๋„๋ก ๋…ธ๋ ฅํ•ด์•ผ์ง€!!

๋Œ“๊ธ€