如何在django中禁用resize textarea?

时间:2014-06-12 18:03:44

标签: html django django-widget

我正在尝试禁用调整大小到django中的textarea小部件,这是我的表单:

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

将resize属性添加到none无法正常工作

3 个答案:

答案 0 :(得分:9)

最简单的方法是添加样式属性:

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

答案 1 :(得分:2)

你的CSS中有这样的东西:

.no-resize {
    resize: none;
}

这在你的Python中添加了类:

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'

答案 2 :(得分:1)

我认为更好的方法是使用style代替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'