I am trying to specify the length in characters on a model field. I cannot get it to render on the page and drop in the size attribute to the input field.
The basic form looks like:
class QuestionForm(ModelForm):
question = forms.CharField(label="Question")
class Meta:
model = Question
fields = ["question"]
I have tried the following:
class QuestionForm(ModelForm):
question = forms.CharField(label="Question",widgets=forms.TextInput(attrs={'size':'50'}))
class Meta:
model = Question
fields = ["question"]
And
class QuestionForm(ModelForm):
question = forms.CharField(label="Question")
class Meta:
model = Question
fields = ["question"]
widgets = {
'question': forms.TextInput(attrs={'size':'50'})
}
And
class QuestionForm(ModelForm): question = forms.CharField(label="Question")
def __init__(self, *args, **kwargs):
super(QuestionForm,self).__init__(*args, **kwargs)
self.fields['question'].widget.attrs['size'] = '50'
The form is part of a formset factory, do you need to do anything different when using that?
ApplicationForm = forms.formset_factory(QuestionForm)
ModelApplicationForm = forms.modelformset_factory(Question, fields=('question',))