Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>The easiest way for your use case is to use CSS</strong>. 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.</p> <p>Example for <code>long_desc</code> field in your ProductForm (when your form does not have a custom prefix):</p> <pre><code>#id_long_desc { width: 300px; height: 200px; } </code></pre> <p><strong>Second approach</strong> is to pass the <code>attrs</code> keyword to your widget constructor.</p> <pre><code>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 </code></pre> <p>It's <a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.Widget.attrs" rel="noreferrer">described in Django documentation</a>.</p> <p><strong>Third approach</strong> is to leave the nice declarative interface of newforms for a while and set your widget attributes in custom constructor.</p> <pre><code>class ProductForm(ModelForm): long_desc = forms.CharField(widget=forms.Textarea) short_desc = forms.CharField(widget=forms.Textarea) class Meta: model = Product # Edit by bryan def __init__(self, *args, **kwargs): super(ProductForm, self).__init__(*args, **kwargs) # Call to ModelForm constructor self.fields['long_desc'].widget.attrs['cols'] = 10 self.fields['long_desc'].widget.attrs['rows'] = 20 </code></pre> <p>This approach has the following advantages:</p> <ul> <li>You can define widget attributes for fields that are generated automatically from your model without redefining whole fields.</li> <li>It doesn't depend on the prefix of your form.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload