Finished
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
{% macro inline_table_row(person, is_editing) %}
|
||||
<tr hx-target="this" hx-swap="outerHTML">
|
||||
{% if is_editing %}
|
||||
{% if is_editing %}
|
||||
<tr id="person-row-{{ person.pk }}" hx-target="this" hx-swap="outerHTML">
|
||||
<td>
|
||||
<input class="form-control" name="name" value="{{ person.name }}">
|
||||
</td>
|
||||
@ -13,8 +13,8 @@
|
||||
<td>
|
||||
<button
|
||||
class="btn btn-outline-success"
|
||||
hx-get="{{ url("table-inline-edit-row", pk=person.pk) }}"
|
||||
hx-vals='{"action": "save"}'
|
||||
hx-post="{{ url("table-inline-edit-row", pk=person.pk) }}"
|
||||
hx-include="#person-row-{{ person.pk }} input"
|
||||
>
|
||||
<i class="bi bi-check-circle-fill"></i>
|
||||
</button>
|
||||
@ -26,7 +26,9 @@
|
||||
<i class="bi bi-x-circle-fill"></i>
|
||||
</button>
|
||||
</td>
|
||||
{% else %}
|
||||
</tr>
|
||||
{% else %}
|
||||
<tr hx-target="this" hx-swap="outerHTML">
|
||||
<td>{{ person.name }}</td>
|
||||
<td>{{ person.address }}</td>
|
||||
<td>{{ person.city }}</td>
|
||||
@ -34,10 +36,11 @@
|
||||
<button
|
||||
class="btn btn-outline-primary"
|
||||
hx-get="{{ url("table-inline-edit-row", pk=person.pk) }}"
|
||||
|
||||
>
|
||||
<i class="bi bi-pencil-square"></i>
|
||||
</button>
|
||||
</td>
|
||||
{% endif %}
|
||||
</tr>
|
||||
</tr>
|
||||
{% endif %}
|
||||
{% endmacro %}
|
||||
|
||||
@ -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,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user