HTMX Tabs Demo

Example Overview

This example demonstrates how to use Bootstrap 5 tabs with HTMX to load tab content (including django_tomselect fields) dynamically without full page reloads. Each tab fires its own HTMX request when selected, and the initially active tab loads on page load. Use this when you want to defer rendering a form until its tab is actually opened.


Key Code Segments

Main Template with Tabs Structure

The main template sets up the Bootstrap 5 tabs and configures HTMX requests for each tab.

  • Purpose: Creates a tabbed interface where each tab triggers an HTMX request to load content dynamically.

  • Key HTMX Attributes:

    • data-hx-get: The URL to fetch content from when the tab is clicked

    • data-hx-trigger: When to trigger the request (on click or load)

    • data-hx-target: Where to place the loaded content


Tab Content Fragment Template

The fragment template defines the content that will be loaded into each tab.


View Functions

The view functions handle both the main page and the tab content fragments.


Implementation Notes

  • Each tab link contains the necessary HTMX attributes to trigger content loading

  • The initially active tab loads its content via the data-hx-trigger="load" attribute

  • Bootstrap’s tab styling and behavior are preserved while enhancing with dynamic content loading