The easiest way to use is to use CSS . This is a language for defining a presentation. Look at the code generated by the form, pay attention to the identifiers for the fields you are interested in and change the appearance of these fields using CSS.
Example for the long_desc field in your ProductForm (when your form does not have a user prefix):
#id_long_desc { width: 300px; height: 200px; }
The second approach is to pass the attrs keyword to your widget constructor.
class ProductForm(ModelForm): long_desc = forms.CharField(widget=forms.Textarea(attrs={'cols': 10, 'rows': 20})) short_desc = forms.CharField(widget=forms.Textarea) class Meta: model = Product
It is described in the Django documentation .
The third approach is to leave the nice declarative interface of newforms for a while and set the widget attributes to the user constructor.
class ProductForm(ModelForm): long_desc = forms.CharField(widget=forms.Textarea) short_desc = forms.CharField(widget=forms.Textarea) class Meta: model = Product
This approach has the following advantages:
- You can define widget attributes for fields that are automatically generated from your model without overriding whole fields.
- It does not depend on the prefix of your form.
zuber Sep 21 '08 at 6:44 2008-09-21 06:44
source share