Python 如何在Django的ImageField中当前重命名、清除和更改标签
我对django比较陌生。我正在使用ImageForm从用户处获取图像路径Python 如何在Django的ImageField中当前重命名、清除和更改标签,python,django,django-models,imagefield,Python,Django,Django Models,Imagefield,我对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
class EditProfileForm(ModelForm):
username = CharField(label='User Name', widget=TextInput(attrs={'class': 'form-control'}), required=True)
image = ImageField(label='Select Profile Image',required = False)
它显示图像小部件,如下所示:
我想重命名标签-当前,清除并更改。[基本上我的整个页面没有小写,所以我想让这些标签文本也用小写,比如当前的clear&change]
有什么方法可以做到这一点吗?你有很多选择
通过使用CSS使文本小写,您可以表现出艺术性
或者,您可以在python/django中更改发送到浏览器的文本
最终,表单字段小部件是通过一个名为render()的函数控制视图的html输出的部件。“ClearableFileInput”小部件的render()函数使用小部件类中的一些变量
您可以使自己的自定义类子类化ClearableFileInput类,并替换自己的小写文本字符串。即:
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)
除去这个老问题,如果您想要比子类化
ClearableFileInput
、创建widgets.py
文件等更简单的东西
如果您已经在forms.py
文件中对ModelForm
进行了子类化,只需修改该表单的\uuuu init\uuuu()
例如:
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"