When creating a form with Django
from django import forms
class SampleForm (forms.Form):
Variable = forms.CharField()
:
I think I'll write like
If you want the number of forms specified by the user,
Array [key] = forms.CharField()
I'm trying to see if I can write like .
Does anyone know?
The django version is 2.1.2.
I look forward to your kind cooperation.
I've solved myself, so I'll leave it as Tips.
The way I used exec in the comment didn't work.
Sample Code
#Import Form Class
from django import forms
# Available empty classes
class EmptyClass (forms.Form):
pass
# Main function
def create_form(myary, *arg):
f = EmptyClass (*arg)
for val in myary:
f.fields [val] = forms.CharField(label=val)
return f
Throw an array of names you want to use in the template into this create_form and they will return the Form class with that field.
You might want to change the arguments to a dictionary array.
I would appreciate it if you could point out any defects or defects.
© 2024 OneMinuteCode. All rights reserved.