Add the update button for a task.
This commit is contained in:
@@ -17,4 +17,6 @@
|
||||
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.
|
||||
** 0.3.2 <2023-07-20 Thu>
|
||||
Add the update button for a task.
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ urlpatterns = [
|
||||
path('', views.index, name='index'),
|
||||
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>/update/', views.task_update, name='task-update'),
|
||||
path('htmx/task-item/<id>/delete/', views.task_delete, name='task-delete'),
|
||||
]
|
||||
|
||||
|
||||
@@ -43,6 +43,22 @@ def task_item(request, id):
|
||||
|
||||
return render(request, "partials/task_item.html", context)
|
||||
|
||||
def task_update(request, id):
|
||||
task = get_object_or_404(Task, id=id)
|
||||
task_form = TaskForm(request.POST or None, instance=task)
|
||||
|
||||
if request.method == "POST":
|
||||
if task_form.is_valid():
|
||||
task_form.save()
|
||||
return redirect("tasks:task-item", id=task.id)
|
||||
|
||||
context = {
|
||||
'task': task,
|
||||
'task_form': task_form,
|
||||
}
|
||||
|
||||
return render(request, "partials/task_form.html", context)
|
||||
|
||||
def task_delete(request, id):
|
||||
task = get_object_or_404(Task, id=id)
|
||||
|
||||
|
||||
@@ -4,6 +4,10 @@
|
||||
<form method="POST">
|
||||
{% csrf_token %}
|
||||
{{ task_form|crispy }}
|
||||
<button type="submit" hx-post=".">Submit</button>
|
||||
{% if task %}
|
||||
<button type="submit" hx-post="{% url 'tasks:task-update' task.id %}">Submit</button>
|
||||
{% else %}
|
||||
<button type="submit" hx-post=".">Submit</button>
|
||||
{% endif %}
|
||||
</form>
|
||||
</div>
|
||||
|
||||
@@ -11,6 +11,11 @@
|
||||
<p class="font-semibold">{{ task.name }}</p>
|
||||
<p>{{ task.description | truncatewords:10 }}</p>
|
||||
|
||||
<button type="button"
|
||||
hx-post="{% url 'tasks:task-update' task.id %}"
|
||||
hx-swap="outerHTML">
|
||||
Update
|
||||
</button>
|
||||
<button type="button"
|
||||
hx-post="{% url 'tasks:task-delete' task.id %}"
|
||||
hx-swap="outerHTML">
|
||||
|
||||
Reference in New Issue
Block a user