Loading...
Loading...
HTMX patterns for Django including partial templates, hx-* attributes, and dynamic UI without JavaScript. Use when building interactive UI, handling AJAX requests, or creating dynamic components.
npx skill4agent add kjnez/claude-code-django htmx-patterns_partial.htmlhx-indicator<button hx-get="/data/" hx-indicator="#spinner">Load</button>request.headers.get("HX-Request")if request.headers.get("HX-Request"): return render(request, "_partial.html", context)_partial.htmlbase.htmlrequest.method == "POST"hx-headers<body>_partial.htmlpage.htmlposts/list.htmlposts/_list.htmlbase.htmlHX-Requestdef my_view(request):
context = {...}
if request.headers.get("HX-Request"):
return render(request, "app/_partial.html", context)
return render(request, "app/full_page.html", context)def create_view(request):
if request.method == "POST":
form = MyForm(request.POST)
if form.is_valid():
obj = form.save()
if request.headers.get("HX-Request"):
return render(request, "app/_item.html", {"item": obj})
return redirect("app:list")
# Return form with errors
if request.headers.get("HX-Request"):
return render(request, "app/_form.html", {"form": form})
else:
form = MyForm()
return render(request, "app/create.html", {"form": form})response["HX-Trigger"] = "itemCreated"<div hx-get="/count/" hx-trigger="itemCreated from:body">response["HX-Redirect"] = reverse("app:detail", args=[obj.pk])response["HX-Retarget"] = "#main"response["HX-Refresh"] = "true"hx-indicator_partial.htmlbase.htmlHX-Requesthx-disabled-elt="this"select_related()prefetch_related()show_urls