API Reference

View Mixins

class django_htmx_tools.views.mixins.IsHtmxRequestMixin[source]

Bases: object

Mixin to check if a request is HTMX

dispatch(request, *args, **kwargs)[source]
Return type:

HttpResponse

Parameters:
  • request (HttpRequest)

  • args (Any)

  • kwargs (Any)

View Decorators

django_htmx_tools.views.decorators.htmx_only_request(view_func)[source]

Decorator that checks if a request is HTMX :type view_func: :param view_func: :return:

Middleware

django_htmx_tools.middleware.htmx.htmx_auth_middleware(get_response)[source]

Middleware to handle HTMX authentication redirects.

When an HTMX request receives a 302 redirect to a login page, this middleware converts it to a 204 response with an HX-Redirect header. This ensures HTMX properly handles the redirect on the client side, preserving the original request path in the ‘next’ query parameter.

Reference: https://www.caktusgroup.com/blog/2022/11/11/how-handle-django-login-redirects-htmx/

Parameters:

get_response – The next middleware or view in the chain.

Returns:

A middleware function that processes HTMX authentication redirects.

django_htmx_tools.middleware.htmx.htmx_vary_middleware(get_response)[source]

Middleware to add Vary headers for HTMX requests.

Adds ‘HX-Request’ to the Vary header for HTMX requests. Required if the server renders the full HTML when the HX-Request header is missing/ false, but renders a fragment of that HTML when HX-Request is true.

Reference: https://htmx.org/docs/#caching

Parameters:

get_response – The next middleware or view in the chain.

Returns:

A middleware function that patches Vary headers for HTMX requests.

Utilities

django_htmx_tools.utils.htmx.is_htmx(request)[source]

Check if a request is HTMX :type request: HttpRequest :param request: :return: True if the request is HTMX, False otherwise

Parameters:

request (HttpRequest)