I'm using Django, Is there a way to check if the current request is from HTMX
How can I check if the current request is from htmx
You can check for HX-Request
in the request headers, as mentioned in another answer. Or you can use the HTMX Django extension and simply check if request.htmx
is True
.
That's it, thank you! –
Curse
@Curse I think it is unnecessary to use third party library to just check for htmx request when you can do it with django request itself no need of any other tool use
HTTP_HX_REQUEST
as i explained in my answer. –
Forespent @Forespent you are correct, but the third party library offers other features like integrating HTMX responses with the Django debugger, which can be extremely helpful. –
Anthropophagy
Yes you can do this in Django by using HTTP_HX_REQUEST
of request.META
like this:
def myview(request):
if request.META.get('HTTP_HX_REQUEST'):
print("HTMX is available")
else:
print("HTMX is not available")
Regardless of the server-side solution you use, you can look for the HX-Request
header. It will be set to true
in all htmx requests. More information can be found here https://htmx.org/docs/#request-header
© 2022 - 2024 — McMap. All rights reserved.