如何解决 Django 表单属性不起作用
How to solve Django form attribute not working
所有表单属性都可以正常工作,但文本区域除外。这是我的代码:
# forms.py
class VineyardForm(forms.ModelForm):
class Meta:
model = Vineyard
fields = [
"name", "text", "wine_rg", "wines", "size", "grapes",
"owner", "visits", "region", "regions", "cover"
]
labels = {
"name": "Vineyard or Property Name",
"text": "Vineyard Description and Information",
"wine_rg": "Wine Region and Country",
}
widgets = {
"name": forms.TextInput(attrs={"placeholder": "Name of your vineyard..."}),
"text": forms.Textarea(attrs={"placeholder": "something"}),
"wine_rg": forms.TextInput(attrs={"placeholder": "Where is your vineyard located: region and country..."}),
}
当我查看页面检查器时,我看到了这段代码:
我认为问题是因为我正在使用 ckeditor。有什么建议吗?
django-ckeditor
包中的 RichTextFormField
编码不当,覆盖了使用 widgets
选项所做的任何更新:
class RichTextFormField(forms.fields.CharField):
def __init__(self, config_name='default', extra_plugins=None, external_plugin_resources=None, *args, **kwargs):
kwargs.update({'widget': CKEditorWidget(config_name=config_name, extra_plugins=extra_plugins,
external_plugin_resources=external_plugin_resources)})
super(RichTextFormField, self).__init__(*args, **kwargs)
但是,您可以在 ModelForm
构造函数中更新小部件属性:
class VineyardForm(forms.ModelForm):
def __init__(self, *args, **kw):
super(VineyardForm, self).__init__(*args, **kw)
self.fields['text'].widget.attrs['placeholder'] = "something"
所有表单属性都可以正常工作,但文本区域除外。这是我的代码:
# forms.py
class VineyardForm(forms.ModelForm):
class Meta:
model = Vineyard
fields = [
"name", "text", "wine_rg", "wines", "size", "grapes",
"owner", "visits", "region", "regions", "cover"
]
labels = {
"name": "Vineyard or Property Name",
"text": "Vineyard Description and Information",
"wine_rg": "Wine Region and Country",
}
widgets = {
"name": forms.TextInput(attrs={"placeholder": "Name of your vineyard..."}),
"text": forms.Textarea(attrs={"placeholder": "something"}),
"wine_rg": forms.TextInput(attrs={"placeholder": "Where is your vineyard located: region and country..."}),
}
当我查看页面检查器时,我看到了这段代码:
我认为问题是因为我正在使用 ckeditor。有什么建议吗?
django-ckeditor
包中的 RichTextFormField
编码不当,覆盖了使用 widgets
选项所做的任何更新:
class RichTextFormField(forms.fields.CharField):
def __init__(self, config_name='default', extra_plugins=None, external_plugin_resources=None, *args, **kwargs):
kwargs.update({'widget': CKEditorWidget(config_name=config_name, extra_plugins=extra_plugins,
external_plugin_resources=external_plugin_resources)})
super(RichTextFormField, self).__init__(*args, **kwargs)
但是,您可以在 ModelForm
构造函数中更新小部件属性:
class VineyardForm(forms.ModelForm):
def __init__(self, *args, **kw):
super(VineyardForm, self).__init__(*args, **kw)
self.fields['text'].widget.attrs['placeholder'] = "something"