Answer the question
In order to leave comments, you need to log in
Django forms, how to dynamically add fields?
class MyForm(forms.Form):
slug = forms.CharField(max_length=50)
class MyForm(forms.Form):
def __init__(self, *args):
super(MyForm, self).__init__(*args)
self.fields.update({'fields': forms.CharField(max_length=50),})
slug = forms.CharField(max_length=50)
Answer the question
In order to leave comments, you need to log in
class MyForm(forms.Form):
def __init__(self, *args, **kwargs):
slugs_cnt = kwargs.pop('slugs_cnt', None)
super(MyForm, self).__init__(*args, **kwargs)
if slugs_cnt:
for i in xrange(1, slugs_cnt + 1):
self.fields['slug_{}'.format(i)] = forms.CharField(max_length=50)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question