I'm trying to create a django form and I want one field for each Region (a class)
I've the following form:
class ShippingForm (forms.Form):
def __init__(self, *args, **kwargs):
super(ShippingForm, self).__init__(*args, **kwargs)
from models import Region
regions = Region.objects.all()
for r in regions:
self.fields['region_%d' % r.id] = forms.IntegerField(attrs={'class' : 'form-control'})
I based the above on this answer
I have 4 Region records created but when I display this form in a template it is blank. What am I missing?
Jay answered this in then comments and I put here to anser / close the question.
Looks like you should use widget= such as
forms.IntegerField(widget=forms.TextInput(attrs={'class' : 'form-control'}))
or
forms.IntegerField(widget=forms.NumberInput(attrs={'class' : 'form-control'}))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.