如何在Django的ImageField中重命名当前,清除和更改标签

Nir*_*pla 6 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)
Run Code Online (Sandbox Code Playgroud)

它显示图像小部件如下:

在此输入图像描述

我想重命名标签 - 目前,清除和更改.[基本上我的整个页面都不是小写的,所以我想让这些标签文本也是小写的,就像当前的那样,清晰和变化].

有没有办法做到这一点?

Rel*_*Rel 9

你有很多选择。

您可以通过使用 CSS 将文本变为小写来提高艺术性。

或者您可以在 python/django 中更改发送到浏览器的文本。

最终,表单字段小部件通过名为 render() 的函数控制 html 输出到视图。“ClearableFileInput”小部件的render() 函数使用来自小部件类的一些变量。

您可以创建您自己的自定义类来继承 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)
Run Code Online (Sandbox Code Playgroud)