Django ModelForm ImageField
Mam model i formularz (formularze.ModelForm), w którym mam ImageField. Model przypomina:
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)
.
.
Formularz towarzyszący:
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())
Ostatnia linijka w formularzu jest komentowana, ponieważ formula.ClearableFileInput () jest domyślnym widżetem dla FileFields i ImageFields.
Teraz, gdy ten formularz jest używany do edycji istniejącego rekordu, obraz w szablonie jest pokazany w następujący sposób:
Currently: <image_url>
Change: <browse_button>
Chciałbym zmienić etykiety tekstowe „Aktualnie” i „Zmień” i zamiast pokazywać image_url, chciałbym pokazać obraz.
Oczywiście mogę skopiować i zmodyfikować kod 'class ClearableFileInput (FileInput)' znaleziony w 'site-packages / django / forms / widgets.py', ale zastanawiam się, czy w ten sposób można to osiągnąć.
Każda pomoc lub sugestia jest mile widziana.
Teraz przejrzałem kod 'class ClearableFileInput (FileInput)'