Dynamically add additional forms for a formset using HTMX and a little bit of hyperscript
Supports deleting and adding rows.
I'm using django-template-partials, django-htmx, htmx and hyperscript
I decorate my views with the @htmx_dynamic_handler
decorator. This decorator allows you to specify the action that triggers the HTMX request and the function that renders the partial.
@htmx_dynamic_handler("action", _function_that_renders_a_partial)
def my_view(request):
action would for instance be the id of the button that was clicked to send the htmx request
In our case, if user clicks the add row button, we render the table row partial with a new form (from formsets)
This way we can keep related partial rendering with the associated view without having to define specific endpoints for partial rendering.
The template rendering code is longer than it needs to be, i manually render forms a bit to represent them in a html table.