liveBook · Manning

Hx-post attribute

As explained in Django in Action

hx-post is an HTMX attribute that initiates an HTTP POST request when its attached HTML element is interacted with.

The hx-post attribute turns an interaction into an HTTP POST request without requiring a full-page navigation. It is especially useful for submitting forms while keeping the response focused on one part of the page. HTMX sends the form data to the URL, then places the returned HTML where hx-target points. section 12.2 section 12.7

Saving an edited room

A form can submit inline with: <button hx-post="{% url 'edit_room_form' room.id %}" hx-target="#room-row-{{room.id}}">Save</button>. Include {% csrf_token %} in the form, because this is a Django POST. If validation succeeds, the view returns show_room.html; HTMX swaps that partial into the targeted row. A Cancel button can use hx-get to restore the non-editing room partial.

How Django in Action covers it

How deep it goes
Explains form awareness, targets, partial responses, validation, saving, and cancellation, but defers broader HTTP implementation details.
Examples
Uses an inline room editor where Save posts an edited Room and replaces its list row.
What it assumes
Assumes basic HTML forms and Django views while demonstrating request methods, validation, CSRF tokens, and partial rendering.
What you can do after
Apply it via HTMX and Django to submit an inline form and update only the relevant HTML fragment.
How it connects
Connects HTTP POST requests with HTML forms, Django views, CSRF protection, partial templates, hx-get, and hx-target.

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.