Change the width of form elements created with ModelForm in Django

后端 未结 4 1623
醉话见心
醉话见心 2020-12-22 19:13

How can I change the width of a textarea form element if I used ModelForm to create it?

Here is my product class:

class ProductForm(ModelForm):
    l         


        
4条回答
  •  余生分开走
    2020-12-22 19:39

    In the event that you're using an add-on like Grappelli that makes heavy use of styles, you may find that any overridden row and col attributes get ignored because of CSS selectors acting on your widget. This could happen when using zuber's excellent Second or Third approach above.

    In this case, simply use the First Approach blended with either the Second or Third Approach by setting a 'style' attribute instead of the 'rows' and 'cols' attributes.

    Here's an example modifying init in the Third Approach above:

    def __init__(self, *args, **kwargs):
        super(ProductForm, self).__init__(*args, **kwargs) # Call to ModelForm constructor
        self.fields['short_desc'].widget.attrs['style'] = 'width:400px; height:40px;'
        self.fields['long_desc'].widget.attrs['style']  = 'width:800px; height:80px;'
    

提交回复
热议问题