widgets = { # 'depart': forms.widget.Select(attrs={'class': 'js-select form-control'}), 这个不知道是什么原因不成功 # 'name': forms.widgets.TextInput(attrs={'class':'form-control'}) }
必须使用
1 def __init__(self, *args, **kwargs): 2 super(UserInfoModelForm, self).__init__(*args, **kwargs) 3 for field in self.fields: 4 if field == "depart": 5 pass 6 self.fields[field].widget.attrs.update({'class': 'js-select form-control'}) 7 else: 8 self.fields[field].widget.attrs.update({'class': 'form-control'})
标签:control,form,样式,self,django,field,attrs,ModelForm,class From: https://www.cnblogs.com/lessonlearn/p/16930854.html