diff --git a/project/main/templates/main/components/inline_table_row.html b/project/main/templates/main/components/inline_table_row.html index 1960443..b5e6733 100644 --- a/project/main/templates/main/components/inline_table_row.html +++ b/project/main/templates/main/components/inline_table_row.html @@ -1,6 +1,6 @@ {% macro inline_table_row(person, is_editing) %} - - {% if is_editing %} + {% if is_editing %} + @@ -13,8 +13,8 @@ @@ -26,7 +26,9 @@ - {% else %} + + {% else %} + {{ person.name }} {{ person.address }} {{ person.city }} @@ -34,10 +36,11 @@ - {% endif %} - + + {% endif %} {% endmacro %} diff --git a/project/main/views/table_inline_edit.py b/project/main/views/table_inline_edit.py index 2171d36..a8e7e58 100644 --- a/project/main/views/table_inline_edit.py +++ b/project/main/views/table_inline_edit.py @@ -1,11 +1,20 @@ from typing import Any -from django.http import Http404 +from django.core.handlers.wsgi import WSGIRequest +from django.http import Http404, HttpRequest, HttpResponse +from django.shortcuts import render from project.main.models import Person from project.main.views.demo_view_base import DemoViewBase +def get_person(pk: int) -> Person: + try: + return Person.objects.get(pk=pk) + except Person.DoesNotExist: + raise Http404("Person not found") + + class TableInlineEditView(DemoViewBase): template_name = "main/table_inline_edit.html" active_section = "table-inline-edit" @@ -30,17 +39,29 @@ class TableInlineEditRowView(DemoViewBase): def get_context_data(self, **kwargs) -> dict[str, Any]: context_data = super().get_context_data(**kwargs) - try: - person = Person.objects.get(pk=kwargs.get("pk")) - except Person.DoesNotExist: - raise Http404("Person not found") - - action = self.request.GET.get("action") + person = get_person(pk=kwargs.get("pk")) + action = self.request.GET.get("action", "edit") context_data.update( { "person": person, - "is_editing": action != "cancel", + "is_editing": action == "edit", } ) return context_data + + def post(self, request: WSGIRequest, *args, **kwargs) -> HttpResponse: + person = get_person(pk=kwargs.get("pk")) + person.name = request.POST.get("name") + person.address = request.POST.get("address") + person.city = request.POST.get("city") + person.save() + + return render( + context={ + "person": person, + "is_editing": False, + }, + template_name=self.template_name, + request=request, + )