A set of extensions for using HTMX with Starlette.
Based on django-htmx by Adam Johnson
Usage:
from starlette.middleware import Middleware
from starlette_htmx.middleware import HtmxMiddleware
app = Starlette(debug=True, ..., middleware=[Middleware(HtmxMiddleware)])
The request objects will then have a request.state.htmx
object which you can
use to test whether the request was made by htmx.
This class provides shortcuts for reading the htmx-specific request headers <https://htmx.org/reference/#request_headers>
__.
True
if the request was made with htmx, otherwise False
.
This is based on the presence of the HX-Request
header.
This allows you to switch behaviour for requests made with htmx like so:
def my_view(request):
if request.htmx:
template_name = "partial.html"
else:
template_name = "complete.html"
return render(template_name, ...)
True
if the request came from an element with the hx-boost
attribute.
Based on the HX-Boosted
header.
The current URL of the browser, or None
for non-htmx requests.
Based on the HX-Current-URL
header.
True
if the request is for history restoration after a miss in the local history cache.
Based on the HX-History-Restore-Request
header.
The user response to hx-prompt <https://htmx.org/attributes/hx-prompt/>
__ if it was used, or None
.
The id
of the target element if it exists, or None
.
Based on the HX-Target
header.
The id
of the triggered element if it exists, or None
.
Based on the HX-Trigger
header.
The name
of the triggered element if it exists, or None
.
Based on the HX-Trigger-Name
header.
The deserialized JSON representtation of the event that triggered the request if it exists, or None
.
This header is set by the event-header htmx extension <https://htmx.org/extensions/event-header/>
__, and contains details of the DOM
event that triggered the request.