HTMX Demo

Example Overview

This example illustrates how to use django_tomselect with HTMX to load a form (and its dropdowns) dynamically on page load, without a full page reload. The key piece is the use_htmx=True config flag, which adjusts the widget’s JavaScript so Tom Select initializes correctly on HTMX-swapped content. Use this pattern when forms are injected into the page after load, such as inline editing or modal dialogs. See the bulk actions example for a more advanced HTMX use case.


Key Code Segments

Forms

The Bootstrap5StylingHTMXForm extends Bootstrap5StylingForm, enabling HTMX integration.

  • Purpose: Configures a field to work with HTMX, enabling dynamic content loading.


HTMX Fragment Template

The htmx_fragment.html template defines the dynamic content that will be loaded into the page.


Main HTMX Template

The htmx.html template renders the page and loads the dynamic fragment via HTMX.


Autocomplete View

The backend for autocomplete remains consistent with the styling examples, but HTMX makes the interaction seamless.