htmx.py 564 B

123456789101112131415161718192021222324
  1. from urllib.parse import urlparse
  2. __all__ = (
  3. 'is_embedded',
  4. 'is_htmx',
  5. )
  6. def is_htmx(request):
  7. """
  8. Returns True if the request was made by HTMX; False otherwise.
  9. """
  10. return 'Hx-Request' in request.headers
  11. def is_embedded(request):
  12. """
  13. Returns True if the request indicates that it originates from a URL different from
  14. the path being requested.
  15. """
  16. hx_current_url = request.headers.get('HX-Current-URL', None)
  17. if not hx_current_url:
  18. return False
  19. return request.path != urlparse(hx_current_url).path