如何自定义 Django ModelForm 字段? [英] How to customize Django ModelForm fields?
本文介绍了如何自定义 Django ModelForm 字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在 Django 中自定义我的 ModelForm.表格如下所示:
Hi I am trying to customize my ModelForm in Django. The form looks like this:
class RegistrationForm(forms.ModelForm):
class Meta:
model = Registration
fields = ['name', 'age', 'details'......]
我正在尝试向不同的字段添加不同的类,以及占位符和无标签.我怎么做?有没有办法在模板中执行此操作?
I am trying to add different classes to different fields, along with placeholders and no labels. How do I do that? Is there a way to carry this out in the templates?
推荐答案
您可以使用小部件向字段添加类和其他参数
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'......]
简要说明:文档
这篇关于如何自定义 Django ModelForm 字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文