Add logic to swap the form with the task via htmx.

This commit is contained in:
2023-07-15 18:04:28 +03:00
parent a0aaef3458
commit c66712d0f4
4 changed files with 46 additions and 3 deletions
+4 -1
View File
@@ -3,8 +3,11 @@ from django.urls import path
from . import views
app_name = 'polls'
app_name = 'tasks'
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'),
]
+33 -1
View File
@@ -1,12 +1,44 @@
from django.shortcuts import render
from django.shortcuts import render, redirect, get_object_or_404
from .models import Task
from .forms import TaskForm
def index(request):
tasks = Task.objects.all()
task_form = TaskForm(request.POST or None)
if request.method == "POST":
if task_form.is_valid():
new_task = task_form.save(commit=False)
new_task.save()
print(f"{new_task.id=}")
return redirect("tasks:task-item", id=new_task.id)
return render(request, "partials/task_form.html", context={
"task_form": task_form
})
context = {
'tasks': tasks,
'task_form': task_form,
}
return render(request, 'tasks/index.html', context)
def create_task_form(request):
task_form = TaskForm()
context = {
'task_form': task_form,
}
return render(request, "partials/task_form.html", context)
def task_item(request, id):
task = get_object_or_404(Task, id=id)
context = {
"task": task
}
return render(request, "partials/task_item.html", context)