[Solved]-How to rename currently, Clear and Change labels in ImageField of Django

10👍

You have many options.

You could be artistic with the use of CSS to make the text lowercase.

or you can change the text that is sent to the browser in python/django.

Ultimately the form fields widget is what controls the html output to the view via a function called render(). The render() function for the “ClearableFileInput” widget uses some variables from the widgets class.

You can make your own custom class subclassing the ClearableFileInput class and substitute your own lowercase text strings. 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)
👤Rel

2👍

Dusting off this old question, if you want something simpler than subclassing ClearableFileInput, creating a widgets.py file, etc.

If you already subclass ModelForm in a forms.py file, just modify the __init__() of that form.

For example:

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"
👤Jarad

Leave a comment