我目前正在尝试创建一个表单,该表单需要多个隐藏的输入以通过javascript收集数据
class NewCasePhotoForm(forms.Form):
case_photo_1 = forms.CharField(required=False)
case_photo_2 = forms.CharField(required=False)
case_photo_3 = forms.CharField(required=False)
case_photo_4 = forms.CharField(required=False)
case_photo_5 = forms.CharField(required=False)
case_photo_6 = forms.CharField(required=False)
case_photo_7 = forms.CharField(required=False)
case_photo_8 = forms.CharField(required=False)
case_photo_9 = forms.CharField(required=False)
case_photo_10 = forms.CharField(required=False)
case_photo_11 = forms.CharField(required=False)
case_photo_12 = forms.CharField(required=False)
case_photo_13 = forms.CharField(required=False)
case_photo_14 = forms.CharField(required=False)
case_photo_15 = forms.CharField(required=False)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for i, field in enumerate(self.fields):
self.fields[field].widget=forms.HiddenInput(
attrs={
'id': 'fileInput{}'.format(i+1)
}
)
是否有更好的方法来进行干燥?
答案 0 :(得分:1)
您可以尝试这样:
class NewCasePhotoForm(forms.Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for i in range(1,16):
self.fields["case_photo_{}".format(i)] = forms.CharField(required=False)