1
votes

I am new to Django and tried making a view that holds a list of forms and handles them. I am familiar with the FormView provided by django's generic views however it only holds one form in it. The idea is that the main form I have is split into a few forms, and then the "mini-forms" are held in a list ("forms"). Since I'm splitting the form into parts and saving a list of forms (for reasons like re-usability and such), the template looks something like this (which does the trick):

...
<form ...>
  {% for form in forms %}
   {{ form.as_p }}
  {% endfor %}
  <input type="submit"...>
</form>
...

I have been messing around with the view that should handle the forms, validate them and will get the data from them. Since I feel like Im making it more complicated than it should really be, I'd like to ask you to give an example of a simple view for that, that Ill develop further (get

1

1 Answers

0
votes

This may be more a comment but I don't have quite enough rep to respond as a comment so I'm putting this in an answer:

If your primary concern is reusability have you considered creating Mixins instead of making a list of forms? an example of this could be:

from django import forms

class NameFormMixin(object):
    your_name = forms.CharField(label='Your name')

class EmailFormMixin(object):
    email = forms.EmailField(label='Your Email')

class NameEmailForm(forms.Form,
                    NameFormMixin,
                    EmailFormMixin):
    pass

you could include your validation functions in the mixins and have general django form functions in the main form class.