Hx Tags

hx_get and hx_post are template tags for setting up HxRequests. They function identically, except hx_get makes a GET request, while hx_post makes a POST request.

Parameters

  • HxRequest Name (required) – The name of the HxRequest being used.

  • Object (optional) - If provided, this object is associated with the HxRequest (similar to Django’s UpdateView )

  • use_full_path (optional) - If set to True, the full path of the url is used. See How To Add GET Parameters To POST Requests

  • Keyword Arguments (kwargs) - Additional arguments passed into the HxRequest. These can be Django Model objects because they are serialized.

Behavior

  • If an object is passed in:
    • It is added as hx_object inside the HxRequest and accessible as self.hx_object.

    • If hx_object_name is not set on the HxRequest, the object is available in the template as hx_object, accessible with {{ hx_object }}.

    • If hx_object_name is set, the object is available in the template as what hx_object_name is set to.

  • Kwargs
    • Accessible in the HxRequest through **kwargs in each method.

    • Available in the template as context variables, unless kwargs_as_context = False is set on the HxRequest, in which case they are stored in hx_kwargs, accessible as {{ hx_kwargs.key }}.

    • kwargs are serialized (see Handling kwargs Serialization) and carried inside the signed token, not as loose query parameters.

Example

<button {% hx_get 'my_hx_request' object=my_instance %}></button>

Is equal to:

<button hx-get="my-website/current-page?hx=<signed-token>"></button>

The hx parameter is a single HMAC-signed token that packs the HxRequest name, the serialized object, and any serialized kwargs. The client can read the token but cannot forge or tamper with it without the project’s SECRET_KEY, so the name/object/kwargs are trusted framework data rather than editable query params.

Note

See Object Serialization for more information on how objects and kwargs are serialized and packed into the token.

hx_url

hx_url takes the same arguments as hx_get / hx_post but returns only the signed URL (the value of the hx query string), not a full hx-get=".." attribute. Use it when you need to build the request attribute yourself – for example on a non-button element, with a different htmx verb, or inside hx-vals / JavaScript.

<div hx-get="{% hx_url 'my_hx_request' object=my_instance %}" hx-trigger="revealed"></div>