liveBook · Manning

HTMX attribute

As explained in Django in Action

An HTMX attribute is a custom HTML attribute that declares an HTTP interaction or specifies how its result affects a page.

An HTMX attribute adds behavior to an HTML tag without requiring a full-page reload. It can invoke an HTTP method, choose when the request runs, and control where the returned HTML goes. In Django, the response can be a partial: a reusable piece of a page rather than the complete document. section 12.2 section 12.7

The main attributes divide the interaction into three practical questions:

  • What request runs? hx-get performs a GET, while hx-post and other method attributes invoke corresponding HTTP methods. section 12.2
  • When does it run? hx-trigger can respond to clicks, page loading, visibility, or events such as key presses. section 12.7
  • Where does the response go? hx-target selects an element with a CSS selector, and hx-swap determines which part changes; innerHTML replaces the target's contents. section 12.2 section 12.7

Replacing a random song

A song-details link can use hx-get to request another song. The Django view returns an HTML partial, while hx-target identifies the song element and hx-swap="innerHTML" replaces its contents. For search-as-you-type, an input can send its value as a query parameter; hx-trigger="keyup changed" limits requests to key presses that change the value. section 12.2 section 12.7

Figure 12.1 Using HTMX attributes to change a link into an action that replaces a portion of the page
figure

Trace how an HTMX-enabled link invokes a Django endpoint and replaces only the relevant page portion. section 12.2

How Django in Action covers it

How deep it goes
Explains request methods, triggers, targets, swaps, partials, and input behavior without descending into library internals.
Examples
Demonstrates random-song replacement, lazy loading, and search-as-you-type with input events.
What it assumes
Assumes familiarity with HTML tags and Django views while introducing partial responses and HTMX request conventions.
What you can do after
Apply it via HTMX to invoke Django views and replace selected page fragments dynamically.
How it connects
Connects HTML attributes with HTTP methods, Django views, partials, CSS selectors, and JavaScript-based interactivity.

Also explained in

Compare how 2 books explain HTMX attribute →

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.