0

I'm working on a Django (1.11.1) app which allows users to request reimbursement for certain purchases. Part of the request form (using django.forms) is an itemized list of purchases. I could, of course, hard code some number of rows into the form and make users submit a separate form if they need more rows. But that seems rather inelegant.

My preferred solution would be to allow users to add rows as necessary via JavaScript, then handle that dynamically in forms. However, everything I've found on the subject relates to situations where the number of elements required can be determined at the time the form is rendered to HTML. It isn't clear to me how to apply such an approach to my situation.

As dynamically adding form elements is extremely common, I'm a bit surprised that such functionality isn't baked into Django already.

By the way, as this is intended to be an incredibly simple app, I don't want a solution that will require a lot of work. If there's no easy solution, it'd be better to just hard-code the form HTML directly into the template.

Scott Severance
  • 943
  • 10
  • 27

1 Answers1

0

Of course it is covered by django. You need to use formset https://docs.djangoproject.com/en/1.11/topics/forms/formsets/

And then in the template you need to add more forms dinamically to that formset, better via javascript. Dynamically adding a form to a Django formset with Ajax

It could result hard to understand all the options that django provides with formsets, but it's quite powerful and the simplest way.

wololoooo
  • 134
  • 8