Trying to include variables as attributes and values into a form field in a Django app template via a partial/component.
There is a somewhat related thread but to my understanding it covers one side of the problem I am facing with django-widget-tweaks and custom tags.
In my case I not only want to assign a dynamic value to a predefined attribute (which I can do with extra template tags as described for instance here) but to first dynamically create an attribute and assign a value to it for the purpose of htmx requests such as hx-get
or hx-post
.
Including partials behaves as expected if I specify the type of htmx request directly in render_field
and pass the value (in this case URL) via include
as well as when I add the attribute and hard code its value using |attr:
like so:
{% include "partials/input-text.html" with field=form.email|attr:"hx-post:/search/" %}
I have tried to apply append_attr
the newly created attribute but I'm seeing AttributeError: 'SafeString' object has no attribute 'as_widget'
and also getting a sensation that I'm missing the point.
Ideally I'm seeking to be able to pass attribute&value as variables (could be via custom template tags), along the lines of {% include "partials/input-text.html" with field|attr:"{{hxaction}}:{{hxurl}}" %}