I have a model and a form (forms.ModelForm) in which I have an ImageField. The model resembles:
class Business(models.Model):
business_name = models.CharField(max_length=128)
.
.
business_image = ImageField(
max_length=128,
upload_to=upload_business_image_handler,
height_field='business_image_height',
width_field='business_image_width'
)
business_image_height = models.IntegerField(blank=True, null=True)
business_image_width = models.IntegerField(blank=True, null=True)
.
.
The accompanying form:
class BusinessForm(forms.ModelForm):
def __init__(self, data=None, files=None, branch_list = None, country_list = None, *args, **kwargs):
super(BusinessForm, self).__init__(data, *args, **kwargs)
# create the MultipleChoiceField choice_list based on language
self.fields['branch'].choices = branch_list
self.fields['country'].choices = country_list
self.postdata = data
self.files = files
business_name = forms.CharField(widget=forms.TextInput(attrs={'size':'50', 'class': 'address'}), required=True)
#business_image = forms.ImageField(widget=forms.ClearableFileInput())
The last line in the form is commented out because forms.ClearableFileInput() is the default widget for FileFields and ImageFields.
Now when this form is used to edit an existing record the image in the template is shown as follows:
Currently: <image_url>
Change: <browse_button>
I would like to change the text-labels 'Currently' and 'Change' and instead of showing the image_url I would like to show the image.
Of course I can copy and modify the code of 'class ClearableFileInput(FileInput)' found in 'site-packages/django/forms/widgets.py' but I am wondering if that's the way to accomplish this.
Any help or suggestion is appreciated.
Now I have looked at the code of 'class ClearableFileInput(FileInput)'
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…