HTMX + Django (and where Interlace fits)
HTMX and Django make a great pair. HTMX turns any element into something that can talk to the server
and swap HTML back in; Django renders that HTML. Add django-htmx
for a few conveniences and you have a genuinely productive stack for adding interactivity without a
single-page app.
This article walks the HTMX + Django pattern honestly, finds the point where you start writing a lot of plumbing, and shows what Interlace does with the same feature. The goal isn't "HTMX bad", it's knowing which layer you want to work at.
The HTMX + Django loop
The core idea: an element declares a request and a target to swap.
<input name="q"
hx-get="{% url 'search' %}"
hx-trigger="input changed delay:200ms"
hx-target="#results">
<ul id="results"></ul>
# views.py
def search(request):
q = request.GET.get("q", "")
results = Tutorial.objects.filter(title__icontains=q)[:5]
return render(request, "_results.html", {"results": results})
<!-- _results.html -->
{% for r in results %}<li>{{ r.title }}</li>{% empty %}<li>No matches</li>{% endfor %}
That's live search in about fifteen lines, and it's easy to follow. This is HTMX at its best: explicit, small, no magic.
Where it starts to add up
Now make it real. The search box grows a category filter, a "sort by" toggle, a loading spinner, and the current query synced to the URL so results are shareable. In HTMX + Django you now juggle:
-
More endpoints and params. Every new control is another
hx-getwith more query params, and the view grows a branch for each. -
Partial templates.
_results.html, maybe_filters.html, each kept in sync with the page it slots into. -
State between requests. The filter, the sort, and the query live in the URL or hidden inputs, and every request has to send and re-read them. Miss one and the UI desyncs.
-
Client state by hand. The spinner, disabled states, and focus you wire with more HTMX attributes or by dropping in Alpine.
None of this is hard. It's just yours to assemble and keep consistent, because HTMX has no opinion about the backend. That's the trade HTMX makes: maximum control, minimum assumptions.
The same feature in Interlace
Interlace takes the opposite trade: it owns the backend piece, so the state and the wiring come with the component.
class Search(Component):
query: str = ""
category: str = ""
# sync both to the URL, one line instead of a plumbing layer
query_params: list = ["query", "category"]
@property
def results(self):
qs = Tutorial.objects.all()
if self.category:
qs = qs.filter(category=self.category)
if self.query:
qs = qs.filter(title__icontains=self.query)
return qs[:5]
def inline_template(self):
return """
<input lace:model.live.debounce.200ms="query">
<select lace:model.live="category">...</select>
<span lace:loading>Searching...</span>
<ul>
{% for r in results %}<li>{{ r.title }}</li>{% empty %}<li>No matches</li>{% endfor %}
</ul>
"""
{% interlace "Search" %}
query and category are just typed attributes; Interlace keeps them across requests and re-renders
when they change. There's no search URL, no partial template, and no hidden inputs threading state.
The loading indicator is a directive, and syncing to the URL is one line, not a new layer.
So which do I use?
-
HTMX when you want the transport and nothing else: progressive enhancement, a handful of explicit endpoints, full control over the HTML, no lock-in, or a non-Django backend.
-
Interlace when the interaction is stateful enough that hand-rolling the state and the endpoints stops being fun, and you'd rather write one Python component.
They share a philosophy, HTML over the wire rather than a virtual DOM, so moving between them feels natural. Interlace is what you reach for when you want that philosophy to include the component and state model, not just the swap. For a feature-by-feature table, see Interlace vs HTMX.