我对django相对来说是个新手。我使用ImageForm从用户获取图像路径。
class EditProfileForm(ModelForm):
username = CharField(label='User Name', widget=TextInput(attrs={'class': 'form-control'}), required=True)
image = ImageField(label='Select Profile Image',required = False)它显示的图像小部件如下:

我想重命名标签-目前,清除和更改。基本上,我的整个页面没有小写,所以我想使这些标签文本也在小写,如目前,明确和改变。
有办法这样做吗?
发布于 2014-07-31 06:49:38
你有很多选择。
您可以通过使用CSS使文本小写而具有艺术性。
或者,您可以更改python/django中发送到浏览器的文本。
最终,表单字段小部件通过一个名为render()的函数控制html输出到视图。"ClearableFileInput“小部件的呈现()函数使用小部件类中的一些变量。
您可以在ClearableFileInput类中创建自己的自定义类,并替换您自己的小写文本字符串。ie:
from django.forms.widgets import ClearableFileInput
class MyClearableFileInput(ClearableFileInput):
initial_text = 'currently'
input_text = 'change'
clear_checkbox_label = 'clear'
class EditProfileForm(ModelForm):
image = ImageField(label='Select Profile Image',required = False, widget=MyClearableFileInput)发布于 2021-03-03 02:01:36
清除这个老问题,如果您想要比子类ClearableFileInput、创建widgets.py文件更简单的东西,等等。
如果您已经在ModelForm文件中子类,只需修改该表单的__init__()即可。
例如:
class EditProfileForm(ModelForm):
username = CharField(label='User Name', widget=TextInput(attrs={'class': 'form-control'}), required=True)
image = ImageField(label='Select Profile Image',required = False)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['image'].widget.clear_checkbox_label = 'clear'
self.fields['image'].widget.initial_text = "currently"
self.fields['image'].widget.input_text = "change"https://stackoverflow.com/questions/25050361
复制相似问题