Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 990
Marco Herrarte Avatar asked Jun 12 '14 18:06

Marco Herrarte


People also ask

How do I stop textarea from resizing?

A simple way to ensure that the control is not resizable is to add an explicit style="resize: none;" attribute on the textarea element using the HTML Form Element Attributes property.

How do I restrict textarea size?

In some cases, there is a need of putting a limit on the size of characters that can be typed in a textarea. So in that case, we can use maxlength attribute to control the number of characters entered in a textarea.

How do I stop textarea from resizing horizontally?

To prevent a text field from being resized, you can use the CSS resize property with its "none" value. After it you can use the height and width properties to define a fixed height and width for your <textarea> element.


2 Answers

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

 widgets = {'vision': forms.Textarea(attrs={'rows':6,
                                            'cols':22,
                                            'style':'resize:none;'}),
    }
like image 158
davko Avatar answered Nov 01 '22 10:11

davko


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'
like image 31
Matt Deacalion Avatar answered Nov 01 '22 08:11

Matt Deacalion