Add the delete button for a task.

This commit is contained in:
2023-07-17 19:25:47 +03:00
parent c66712d0f4
commit a929846d13
4 changed files with 19 additions and 1 deletions
+2
View File
@@ -15,4 +15,6 @@
Add the htmx (CDN version). Add the htmx (CDN version).
Add the task form. Add the task form.
Add logic to swap the form with the task via htmx. Add logic to swap the form with the task via htmx.
** 0.3.1 <2023-07-17 Mon>
Add the delete button for a task.
+1
View File
@@ -9,5 +9,6 @@ urlpatterns = [
path('', views.index, name='index'), path('', views.index, name='index'),
path('htmx/create-task-form/', views.create_task_form, name='create-task-form'), path('htmx/create-task-form/', views.create_task_form, name='create-task-form'),
path('htmx/task-item/<id>/', views.task_item, name='task-item'), path('htmx/task-item/<id>/', views.task_item, name='task-item'),
path('htmx/task-item/<id>/delete/', views.task_delete, name='task-delete'),
] ]
+9
View File
@@ -1,4 +1,5 @@
from django.shortcuts import render, redirect, get_object_or_404 from django.shortcuts import render, redirect, get_object_or_404
from django.http.response import HttpResponse, HttpResponseNotAllowed
from .models import Task from .models import Task
from .forms import TaskForm from .forms import TaskForm
@@ -42,3 +43,11 @@ def task_item(request, id):
return render(request, "partials/task_item.html", context) return render(request, "partials/task_item.html", context)
def task_delete(request, id):
task = get_object_or_404(Task, id=id)
if request.method == "POST":
task.delete()
return HttpResponse("")
return HttpResponseNotAllowed([ "POST" ])
+7 -1
View File
@@ -1,4 +1,4 @@
<div class="p-2 border rounded-lg inline-flex gap-2 my-2 bg-gray-200"> <div hx-target="this" class="p-2 border rounded-lg inline-flex gap-2 my-2 bg-gray-200">
{% if task.status == "TODO" %} {% if task.status == "TODO" %}
<p class="font-semibold text-green-500"> <p class="font-semibold text-green-500">
{% elif task.status == "DOING" %} {% elif task.status == "DOING" %}
@@ -10,4 +10,10 @@
</p> </p>
<p class="font-semibold">{{ task.name }}</p> <p class="font-semibold">{{ task.name }}</p>
<p>{{ task.description | truncatewords:10 }}</p> <p>{{ task.description | truncatewords:10 }}</p>
<button type="button"
hx-post="{% url 'tasks:task-delete' task.id %}"
hx-swap="outerHTML">
Delete
</button>
</div> </div>