且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何自定义 Django ModelForm 字段?

更新时间:2023-02-12 15:44:41

您可以使用小部件向字段添加类和其他参数

You can add class and other parameters to a field using widget

   class RegistrationForm(forms.ModelForm):
      name = forms.CharField(label=' ',widget=forms.TextInput(attrs={'class':'textClass','placeholder':'Enter Name'}))
      details = forms.CharField(widget=forms.Textarea(attrs={'class':'special','maxlength':'100'}))
      class Meta:
         model = Registration
         fields = ['name', 'age', 'details'......]

简要说明:文档