2012-08-10 98 views
1

使用django.contrib.comments,我定義了一個自定義評論應用程序。我想覆蓋文本區域小部件,使文本框看起來更小。Django Contrib評論:如何覆蓋評論的textarea小部件?

所以我創建的是:

#forms.py 
class CustomCommentForm(CommentForm): 
    #...otherstuff... 

    comment = forms.CharField(label=_('Comment'), 
     widget=forms.Textarea(attrs={'rows':4}), 
     max_length=COMMENT_MAX_LENGTH) 

但我真的不希望有重新定義註釋字段。我只想重新定義該字段使用的小部件。也就是說,它似乎只有ModelForms可以做的:

class Meta: 
    widgets = { 
     'comment': Textarea(attrs={'rows': 4}), 
    } 

有沒有一種方法來重新定義小部件而不重新定義字段?或者我應該只使用CSS設置高度?

+3

我會使用CSS - 快速和優雅。 – Thomas 2012-08-10 12:23:41

回答

1

對於模型表格的Meta類,您只能使用widgets選項。

但是,您不需要重新定義整個comment字段。相反,請覆蓋表單的__init__方法並在那裏更改字段的widget

class CustomCommentForm(CommentForm): 
    #...otherstuff... 

    def __init__(self, *args, **kwargs): 
     super(CustomCommentForm, self).__init__(*args, **kwargs) 
     self.fields['comment'].widget = forms.Textarea(attrs={'rows':4})