How can I change the width of a textarea form element if I used ModelForm to create it?
Here is my product class:
class ProductForm(ModelForm): long_desc = forms.CharField(widget=forms.Textarea) short_desc = forms.CharField(widget=forms.Textarea) class Meta: model = Product
And the template code…
{% for f in form %} {{ f.name }}:{{ f }} {% endfor %}
f is the actual form element…
The easiest way for your use case is to use CSS. It’s a language meant for defining presentation. Look at the code generated by form, take note of the ids for fields that interest you, and change appearance of these fields through CSS.
Example for
long_descfield in your ProductForm (when your form does not have a custom prefix):Second approach is to pass the
attrskeyword to your widget constructor.It’s described in Django documentation.
Third approach is to leave the nice declarative interface of newforms for a while and set your widget attributes in custom constructor.
This approach has the following advantages: