Getting Started¶
Get up and running with django-htmx-nav in under 3 minutes.
This guide walks through setting up your first partial view with render_nav and synchronizing your first peripheral navigation region with Swap.
1. Installation¶
Install django-htmx-nav from PyPI:
pip install django-htmx-nav
(Optional) If you want to use the visual swap debugging tools during development, add "htmx_nav" to INSTALLED_APPS in your settings.py:
INSTALLED_APPS = [
...,
"htmx_nav",
]
3. Synchronizing Peripheral Regions (Swap)¶
When HTMX swaps #content, the main area updates, but peripheral layout elements (such as active sidebar indicators or breadcrumbs) remain stuck on the previous route.
Use Swap to append synchronized out-of-band updates in the same response:
# views.py
from django.shortcuts import get_object_or_404
from htmx_nav import Swap, render_nav
from .models import Project
def project_detail(request, pk):
project = get_object_or_404(Project, pk=pk)
return render_nav(
request,
"pages/project_detail.html",
{"project": project},
# Peripheral regions synced out-of-band:
swaps=[
# Auto-wraps nav/_sidebar.html in <div id="sidebar" hx-swap-oob="innerHTML">
Swap("nav/_sidebar.html", {"active_pk": project.pk}, target_id="sidebar"),
# Synchronizes the breadcrumb trail:
Swap("nav/_breadcrumbs.html", {"project": project}, target_id="breadcrumbs"),
],
title=project.name,
)
With this single addition, HTMX updates #content with the primary response and simultaneously swaps #sidebar and #breadcrumbs out-of-band. The URL remains the single source of truth without state drift.
What’s Next?¶
Now that your first view and swap are wired up, explore the topic guides to build production-grade architectures:
Dynamic Targeting & Nested Navigation: Coordinate nested tabs, subtabs, and conditional swaps using the unified
Targetcondition vocabulary.Application Shells & Class-Based Views: Eliminate repetitive boilerplate across 20+ views with
make_shell_rendererandmake_shell_view_mixin.Visual Swap Debugging: Highlight DOM elements with visual animations as they swap during development.
API Reference: Precise function signatures, parameters, and return types.