liveBook · Manning

Click-to-edit

As explained in Django in Action

Click-to-edit is an inline-editing interaction pattern in which displayed data changes into an editable form after the user clicks it or a control.

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

How Django in Action covers it

How deep it goes
Builds the pattern through views, partial templates, forms, requests, validation, saving, replacement, and cancellation.
Examples
Demonstrates editing Room names in a venue's room list with Save and Cancel controls.
What it assumes
Assumes Django models, views, templates, routes, forms, authentication, and basic HTTP methods such as GET and POST.
What you can do after
Build it from scratch with HTMX by swapping display and form partials, validating submitted data, saving it, and restoring canceled edits.
How it connects
Connects HTMX GET and POST requests with Django views, model forms, partial templates, authentication, and CSRF protection.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.