我试图让占位符出现在表单字段中,但我的表单字段有问题。有什么想法吗?我有以下课程:
class EditField(forms.CharField):
def __init__(self, *args, **kwargs):
super(EditField, self).__init__(*args, **kwargs)
self.widget = forms.Textarea(attrs={'id':'editor'})
self.label = _('content')
class EditField_typeA(EditField):
def __init__(self, *args, **kwargs):
super(EditField_typeA, self).__init__(*args, **kwargs)
def clean(self, value):
if not (len(re.sub('[ ]{2,}', ' ', value)) < settings.FORM_MIN):
raise forms.ValidationError(_('content must be %s') % settings.FORM_MIN)
return value
class FinalForm(forms.Form):
foo = FooField()
bar = BarField()
text = EditField_typeA()
def __init__(self, data=None, user=None, *args, **kwargs):
super(FinalForm, self).__init__(data, *args, **kwargs)
## THIS IS THE PART THAT ISN'T WORKING
self.fields['text'].widget.attrs['placeholder'] = 'Fill this in'
if int(user.reputation) < settings.CAPTCHA_IF_REP_LESS_THAN and not (user.is_superuser or user.is_staff):
spam_fields = call_all_handlers('create_anti_spam_field')
if spam_fields:
spam_fields = dict(spam_fields)
for name, field in spam_fields.items():
self.fields[name] = field
self._anti_spam_fields = spam_fields.keys()
else:
self._anti_spam_fields = []
我猜我用错了小部件,或者在错误的地方。找不到小部件文档的正确部分来解释我做错了什么。