The headers sent by htmx are documented here.
The following code show how you access them in Django. This view behaves differently for htmx requests compared to normal requests:
def headers_demo(request):
if request.headers.get("Hx-Request", False):
current_url = request.headers["Hx-Current-URL"]
return HttpResponse(f"This is a response to a request sent by htmx from {current_url}<br>")
return TemplateResponse(request, "headers.html", {})
- The django-htmx package has a convenience middleware to make it easier to read htmx headers.