Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename currently, Clear and Change labels in ImageField of Django

I am relatively new to django. I am using ImageForm to get the image path from the user.

class EditProfileForm(ModelForm):
    username = CharField(label='User Name', widget=TextInput(attrs={'class': 'form-control'}), required=True)
    image = ImageField(label='Select Profile Image',required = False)

It is showing the image widget as follows:

enter image description here

I want to rename the labels - Currently, Clear and Change. [Basically my entire page is no lower case, so I wanted to make these label text also in lower case like currently, clear & change].

Is there any way to do this?

like image 406
Niraj Chapla Avatar asked Jul 31 '14 03:07

Niraj Chapla


People also ask

How do I remove a form label in Django?

In __init__ method set your field label as empty. This will remove label text. Save this answer.

What is default storage in Django?

By default, Django stores files locally, using the MEDIA_ROOT and MEDIA_URL settings. The examples below assume that you're using these defaults. However, Django provides ways to write custom file storage systems that allow you to completely customize where and how Django stores files.


1 Answers

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)
like image 65
Rel Avatar answered Oct 04 '22 00:10

Rel