Click-to-edit is an inline-editing pattern: displayed data becomes an editable form when the user clicks an Edit link or control. HTMX swaps only the affected page fragment, avoiding a full-page navigation. section 12.5 section 12.7
Editing rooms for a venue
A venue page lists its rooms. Each room displays its name and an Edit link. Clicking Edit swaps that list item for a RoomForm; Save posts the form and swaps the item back to its updated display, while Cancel restores the original partial. The views require login and restrict room access to the current user's venue. section 12.5
Listing 12.16 An HTMX view for handling a room-editing form
# RiffMates/bands/views.py
...
from bands.forms import RoomForm
...
@login_required #1
def edit_room_form(request, room_id): #2
room = get_object_or_404(Room, id=room_id,
venue__userprofile=request.user.userprofile) #3
if request.method == "POST": #4
form = RoomForm(request.POST, instance=room)
if form.is_valid(): #5
form.save()
data = {
"room": room,
}
return render(request, "partials/show_room.html",
data) #6
else:
form = RoomForm(instance=room) #7
data = {
"room": room,
"form": form,
}
return render(request, "partials/edit_room_form.html", data) #8
Inspect the complete HTMX view that separates GET form display, POST validation and saving, and successful display-partial rendering. section 12.5
