[Solved]-How to disable resize textarea in django?

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

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

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'

Leave a comment