1

There are several writeups on how to use javascript to add forms dynamically to a Django formset. For example this or this.

I just learned about htmx.

How can I add forms dynamically to a Django formset using htmx instead of hand-coded javascript? I thought maybe I could use click-to-load, but it seems odd to call back to the server to get an empty row. Maybe this just isn't an htmx thing.

dfrankow
  • 18,326
  • 38
  • 134
  • 193

1 Answers1

2

With HTMX, instead of the complication of formsets, you can dynamically add forms, and swap in the resulting model instance on submit.

This example todo app provides a demonstration of the approach. It allows you to add additional items by appending a form, and upon submitting successfully, swaps in the model instance resulting from that form submission. The end result is much like using formsets.

djangomachine
  • 437
  • 2
  • 10