How To Detect And Inspect An HxRequest

The routing data (name, object, kwargs) is signed inside a single hx token, so it is no longer readable as loose hx_request_name / object / ___kwarg query parameters. To work with an inbound request from your own view code, use the helpers in hx_requests.utils.

Checking If A Request Is An HxRequest

is_hx_request(request) returns True when the request carries a valid signed token — i.e. it is bound for a registered HxRequest.

This is different from is_htmx_request(request), which only checks the HX-Request header and is True for any htmx request. The common use is telling a plain htmx request (sort / filter / paginate) apart from one routed to an HxRequest, so the plain one can fall through to the underlying view:

from hx_requests.utils import is_htmx_request, is_hx_request

def dispatch(self, request, *args, **kwargs):
    # Plain htmx (no hx token) -> let the underlying view handle it.
    if is_htmx_request(request) and not is_hx_request(request):
        return SomeListView.dispatch(self, request, *args, **kwargs)
    return super().dispatch(request, *args, **kwargs)

Reading The Name, Object, And Kwargs

When you need the request’s data before dispatch runs:

  • get_hx_request_name(request) returns the verified name (or None).

  • get_hx_payload(request) returns the full verified payload, {"name": ..., "object": ..., "kwargs": ...} (or None).

from hx_requests.utils import get_hx_request_name, get_hx_payload

name = get_hx_request_name(request)

payload = get_hx_payload(request)
if payload:
    name = payload["name"]

The object and kwargs in the payload are still in serialized form. Run them through deserialize / deserialize_kwargs to get live values:

from hx_requests.utils import get_hx_payload, deserialize, deserialize_kwargs

payload = get_hx_payload(request)
if payload:
    obj = deserialize(payload["object"]) if payload["object"] else None
    kwargs = deserialize_kwargs(**payload["kwargs"])

Note

All of these helpers return None / False on a missing or tampered token — they never raise — so they are safe to call on any request.