Every few months, a new JavaScript framework appears and promises to solve frontend development. Meanwhile, Django developers who adopted HTMX two years ago are still shipping features faster than teams with 5-person frontend squads.
HTMX lets you add dynamic, interactive behavior to Django templates using HTML attributes — no JavaScript, no build step, no npm, no webpack. This isn't a theory post. Here are 5 practical patterns you can add to any Django project today.
What is HTMX?
HTMX is a 14KB JavaScript library that extends HTML with attributes like hx-get, hx-post, hx-swap, and hx-trigger. These attributes let any HTML element make AJAX requests and swap content on the page.
Add it to your base template:
<script src="https://unpkg.com/htmx.org@2.0.0"></script>
That's the entire setup. No npm install, no configuration files, no bundler.
Pattern 1: Real-Time Search
The most common HTMX pattern. Users type, results appear instantly — no page reload.
Template:
<input type="search"
name="q"
hx-get="{% url 'search_results' %}"
hx-trigger="input changed delay:300ms"
hx-target="#results"
placeholder="Search users...">
<div id="results">
{% include "partials/user_list.html" %}
</div>
View:
def search_results(request):
q = request.GET.get("q", "")
users = User.objects.filter(
Q(username__icontains=q) | Q(email__icontains=q)
)[:20] if q else User.objects.none()
return render(request, "partials/user_list.html", {"users": users})
Partial template (partials/user_list.html):
{% for user in users %}
<div class="user-card">
<strong>{{ user.username }}</strong>
<span>{{ user.email }}</span>
</div>
{% empty %}
<p>No results found.</p>
{% endfor %}
The delay:300ms debounces input so you're not firing a request on every keystroke. The view returns an HTML fragment, not a full page. Django's template system handles the rendering.
Pattern 2: Inline Editing
Click a field to edit it in place. Save on blur or Enter.
<!-- Display mode -->
<span hx-get="{% url 'edit_field' item.id %}"
hx-trigger="click"
hx-swap="outerHTML"
class="editable">
{{ item.name }}
</span>
# Edit form view
def edit_field(request, item_id):
item = get_object_or_404(Item, id=item_id)
if request.method == "POST":
item.name = request.POST.get("name", item.name)
item.save()
return render(request, "partials/item_display.html", {"item": item})
return render(request, "partials/item_edit.html", {"item": item})
<!-- Edit mode (partials/item_edit.html) -->
<form hx-post="{% url 'edit_field' item.id %}"
hx-swap="outerHTML">
{% csrf_token %}
<input type="text" name="name" value="{{ item.name }}"
autofocus
hx-trigger="blur from:input, keyup[key=='Enter'] from:input">
</form>
No modal dialogs. No form state management. No controlled components. Just HTML.
Pattern 3: Infinite Scroll
Load more items as the user scrolls down.
{% for item in items %}
<div class="item">{{ item.title }}</div>
{% endfor %}
{% if has_more %}
<div hx-get="{% url 'item_list' %}?page={{ next_page }}"
hx-trigger="revealed"
hx-swap="outerHTML">
Loading more...
</div>
{% endif %}
def item_list(request):
page = int(request.GET.get("page", 1))
per_page = 20
items = Item.objects.all()[((page-1)*per_page):(page*per_page + 1)]
has_more = len(items) > per_page
return render(request, "partials/item_list.html", {
"items": items[:per_page],
"has_more": has_more,
"next_page": page + 1,
})
hx-trigger="revealed" fires when the element scrolls into view. The loading div replaces itself with the next page of results, including a new loading trigger if there are more items.
Pattern 4: Delete with Confirmation
Delete an item with a confirmation step, no JavaScript alerts.
<button hx-delete="{% url 'delete_item' item.id %}"
hx-confirm="Delete '{{ item.name }}'? This cannot be undone."
hx-target="closest .item-row"
hx-swap="outerHTML swap:300ms"
class="btn-danger">
Delete
</button>
from django.views.decorators.http import require_http_methods
@require_http_methods(["DELETE"])
def delete_item(request, item_id):
item = get_object_or_404(Item, id=item_id)
item.delete()
return HttpResponse("") # Empty response removes the element
The swap:300ms gives CSS time to animate the removal. The empty response makes the target element disappear.
Pattern 5: Form Validation Without Page Reload
Validate individual fields as the user fills them out.
<form method="post" action="{% url 'register' %}">
{% csrf_token %}
<div>
<label>Username</label>
<input name="username"
hx-post="{% url 'validate_username' %}"
hx-trigger="blur"
hx-target="next .validation-msg">
<span class="validation-msg"></span>
</div>
<div>
<label>Email</label>
<input name="email" type="email"
hx-post="{% url 'validate_email' %}"
hx-trigger="blur"
hx-target="next .validation-msg">
<span class="validation-msg"></span>
</div>
<button type="submit">Register</button>
</form>
def validate_username(request):
username = request.POST.get("username", "")
if len(username) < 3:
return HttpResponse('<span class="error">Username must be at least 3 characters</span>')
if User.objects.filter(username=username).exists():
return HttpResponse('<span class="error">Username already taken</span>')
return HttpResponse('<span class="success">Available</span>')
Each field validates independently, on blur, with no JavaScript validation library.
Why This Beats a React Frontend
| Django + HTMX | Django + React | |
|---|---|---|
| Setup time | 1 line (script tag) | 30+ min (npm, webpack, API) |
| Files per feature | 1 view + 1 partial | Component + API endpoint + types |
| State management | Server (session/DB) | Client (Redux/Context/Zustand) |
| SEO | Free (server-rendered) | Requires SSR setup |
| Team skills needed | Python + HTML | Python + JavaScript + React |
I'm not saying React is bad. I'm saying most Django projects don't need it.
Get Started Faster
If you want a pre-configured Django + HTMX project with Tailwind CSS, Alpine.js, authentication, admin customization, and deployment configs already set up, check out the Django HTMX Starter Kit. Skip the boilerplate and start building features.
Are you using HTMX in production? What patterns work best for your team? Share in the comments.