Marco Herrarte
Marco Herrarte

Reputation: 1620

How to disable resize textarea in django?

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

Upvotes: 12

Views: 5863

Answers (3)

Sergi Ramón
Sergi Ramón

Reputation: 213

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'

Upvotes: 1

davko
davko

Reputation: 459

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

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

Upvotes: 18

Matt
Matt

Reputation: 10322

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'

Upvotes: 2

Related Questions