11

I'm trying to disable resize to the textarea widget in django, this is my form:

class VForm(forms.ModelForm):
    class Meta:
        model = Visions
        widgets = {'vision': forms.Textarea(attrs={'rows':6,
                                                   'cols':22,
                                                   'resize':'none'}),
        }

Adding the resize property to none isn't working

Marco Herrarte
  • 1,540
  • 5
  • 21
  • 42

3 Answers3

17

The simplest way to do this is to add a style attribute:

 widgets = {'vision': forms.Textarea(attrs={'rows':6,
                                            'cols':22,
                                            'style':'resize:none;'}),
    }
davko
  • 449
  • 2
  • 13
2

Something like this in your CSS:

.no-resize {
    resize: none;
}

And this in your Python to add the class:

class VForm(forms.ModelForm):
    class Meta:
        model = Visions

    def __init__(self, *args, **kwargs):
        """
        This has been overridden to customise the textarea form widget.
        """
        super(VForm, self).__init__(*args, **kwargs)

        self.fields['vision'].widget.attrs['class'] = 'no-resize'
Matt
  • 8,758
  • 4
  • 35
  • 64
1

I think the better way is using style instead of class:

class VForm(forms.ModelForm):
    class Meta:
        model = Visions
    def __init__(self, *args, **kwargs):        
        super(VForm, self).__init__(*args, **kwargs)
        self.fields['vision'].widget.attrs['style'] = 'resize:none'
IKavanagh
  • 6,089
  • 11
  • 42
  • 47
Sergi Ramón
  • 185
  • 1
  • 4