views:

2730

answers:

2

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...

+23  A: 

The easiest way for your use case is to use CSS. It's a language meant for defining presentation. Look at the code generted by form, take note of ids for fields that interest you, and change appearance of these fields through CSS.

Example for long_desc field in your ProductForm (when your form does not have a custom prefix):

#id_long_desc {
    width: 300px;
    height: 200px;
}

Second approach is to pass 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'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.

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['cols'] = 20

This approach has the following advantages:

  • You can define widget attributes for fields that are generated automatically from your model without redefining whole fields.
  • It doesn't depend on the prefix of your form.
zuber
The second one worked perfectly
joshhunt
Thanks for this answer - I've just implemented this in a project that I'm working on.
Brenton C
Option 3 is very useful. Perhaps the example could also show that the fields do not need to be defined in the Form, yet can still override fields defined in the Model that come through automatically.
Dan Breen
+4  A: 

Excellent answer by zuber, but I believe there's an error in the example code for the third approach. The constructor should be:

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['cols'] = 20

The Field objects have no 'attrs' attributes, but their widgets do.

bryan
That really should be an edit, rather than its own answer (its better with the SO model), but you dont have the sufficient reputation to make edits...
joshhunt
I have merged the edit in, we can keep this answer also if someone wants to vote it up.
Ólafur Waage
Sorry, you're right -- I should have left a comment instead. I'll do better next time :)
bryan