HX Tags
There are two template tags to use to setup HXRequests
: hx_get
and hx_post
. They are identical except that one makes a get request and one makes a post request.
hx_get
Example from quickstart
{% load hx_tags %}
<div id="user_info_target"></div>
<button {% hx_get 'get_user_info' %}
hx-target="#user_info_target"
type="button">
Click Me
</button>
hx_get
takes in:The name of the
HXRequest
that is being usedAn object if there is one that is associted with the
HXRequest
(similar to Django’s UpdateView )And kwargs
- Notes:
If an object is passed in it is added as an attribute to the
HXRequest
ashx_object
and can be accessed asself.hx_object
If
hx_object_name
is not set on theHXRequest
, the object is passed into the template (GET_template
forget
requests orPOST_template
forpost
requests) ashx_object
and can be accessed in the template as{{ hx_object }}
Kwargs that are passed in are accessible in the
HXRequest
(through**kwargs
in each method). They get passed into the template as part of the context, unlesskwargs_as_context
is set to False then they are put in ashx_kwargs
and can be accessed as{{ hx_kwargs.key }}
Besides for primitive types the object and the kwargs can be a
model_instance
(an instance of a Django model) because amodel_instance
gets serialized byhx_get
.
Warning
Objects passed into hx_get
, whether as the ‘object’ or in kwargs must be Django model_instance
. Other objects will not be serialized/ deserialized (primitives are of course okay).
hx_post
Same as above except that a post request will be made instead of a get request.