0

How to make a django form to take a dynamic number of inputs from user in django, resulting in dynamic number of form fields.

I need forms.Form, not forms.ModelForm, because saving to model is not required.

class form_name2(forms.Form):
    a1 = forms.CharField(
        widget=forms.TextInput(),
        max_length=100,
        label="1",
        required=False)
    a2 = forms.CharField(
        widget=forms.TextInput(),
        max_length=100,
        label="2",
        required=False)
    a3 = forms.CharField(
        widget=forms.TextInput(),
        max_length=100,
        label="3",
        required=False)
Ralf
  • 16,086
  • 4
  • 44
  • 68
Nitish Singh
  • 215
  • 1
  • 2
  • 15
  • I would look at formsets https://docs.djangoproject.com/en/2.1/topics/forms/formsets/ – Taylor Aug 09 '18 at 17:02
  • i want to make n no. of fields according to users requirement, that user can increase,anyone please help me out here – Nitish Singh Aug 09 '18 at 17:03
  • formsets allow you to make n number of fields otherwise you would need to use javascript – Taylor Aug 09 '18 at 17:04
  • using formsets i can make dynamic no. of forms...but i need to make dynamic no of fields with auto incremented 1,2,3,4,.... lyk this – Nitish Singh Aug 09 '18 at 17:07
  • It can be done with formsets. Here are two articles on how to do it with formsets and javascript. Google is your friend, this is a repeat of a question asked many times on here. https://stackoverflow.com/questions/6142025/dynamically-add-field-to-a-formhttps //www.caktusgroup.com/blog/2018/05/07/creating-dynamic-forms-django/ – Taylor Aug 09 '18 at 17:12

0 Answers0