1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

DjangoでTodoアプリ作成その3DeleteView

Posted at

DeleteView

データを削除するときに適したテンプレート

テンプレートの作成

template/delete.html
{% extends 'base.html' %}

{% block content %}
    <form action="" method="POST">{% csrf_token %}
        <input type="submit" value="削除します">
    </form>
{% endblock content %}

urlsの編集

todo.urls.py
from django.urls import path
from .views import TodoList, TodoDetail, TodoCreate, TodoDelete

urlpatterns = [
    path('list/', TodoList.as_view(), name='list'),
    path('detail/<int:pk>', TodoDetail.as_view(), name='detail'),
    path('create/', TodoCreate.as_view(), name='create'),
    path('delete/<int:pk>', TodoDelete.as_view(), name='delete'),
]

views.pyの編集

views.py
from django.views.generic import ListView, DetailView, CreateView, DeleteView

class TodoDelete(DeleteView):
    template_name = 'delete.html'
    model = TodoModel
    success_url = reverse_lazy('list')
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?