[英]Django form with fields based on queryset
I'm trying to create a django form and I want one field for each Region (a class) 我正在尝试创建Django表单,并且每个区域都需要一个字段(一个类)
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. 我创建了4个区域记录,但是当我在模板中显示此表单时,它为空白。 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 看起来您应该使用widget =
forms.IntegerField(widget=forms.TextInput(attrs={'class' : 'form-control'}))
or 要么
forms.IntegerField(widget=forms.NumberInput(attrs={'class' : 'form-control'}))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.