(Django 기초) articleapp 구현 - Delete

Posted by : at

Category : Django



# articleapp\views.py

# ...

@method_decorator(article_ownership_required, 'get')
@method_decorator(article_ownership_required, 'post')
class ArticleDeleteView(DeleteView):
    model = Article
    context_object_name = 'target_article'
    template_name = 'articleapp/delete.html'
    success_url = reverse_lazy('articleapp:list')
<!-- articleapp\delete.html -->
{% extends 'base.html' %}
{% load bootstrap4 %}

{% block content %}

    <div style="text-align: center; max-width: 500px; margin: 4rem auto">
        <div class="mb-4">
            <h4>Delete Article : {{ target_article.title }}</h4>
        </div>
        <div>
            <form action="{% url 'articleapp:delete' pk=target_article.pk %}" method="post">
                {% csrf_token %}
                {% bootstrap_form form %}
                <input type="submit" class="btn btn-danger rounded-pill col-6 mt-3">
            </form>
        </div>
    </div>

{% endblock %}
<!-- articleapp\detail.html -->
{% extends 'base.html' %}

{% block content %}

    <div style="text-align: center; max-width: 500px; margin: 4rem auto">
        <h1>
            {{ target_article.title }}
        </h1>

        <img src="{{ target_article.image.url }}" alt="">

        <p>
            {{ target_article.content }}
        </p>

        <a href="{% url 'articleapp:update' pk=target_article.pk %}">
            <p>Update Article</p>
        </a>

        <a href="{% url 'articleapp:delete' pk=target_article.pk %}">
            <p>Delete Article</p>
        </a>

    </div>

{% endblock %}
# articleapp\urls.py

# ...
urlpatterns = [
    path('list/', TemplateView.as_view(template_name='articleapp/list.html'), name='list'),

    path('create/', ArticleCreateView.as_view(), name='create'),
    path('detail/<int:pk>', ArticleDetailView.as_view(), name='detail'),
    path('update/<int:pk>', ArticleUpdateView.as_view(), name='update'),
    path('delete/<int:pk>', ArticleDeleteView.as_view(), name='delete'),
]

About Taehyung Kim

안녕하세요? 8년차 현업 C++ 개발자 김태형이라고 합니다. 😁 C/C++을 사랑하며 다양한 사람과의 협업을 즐깁니다. ☕ 꾸준한 자기개발을 미덕이라 생각하며 노력중이며, 제가 얻은 지식을 홈페이지에 정리 중입니다. 좀 더 상세한 제 이력서 혹은 Private 프로젝트 접근 권한을 원하신다면 메일주세요. 😎

Star
Useful Links