Hello,
I have a form which allow the user to upload a logo. As I display the logo on the screen () I don’t need to have the “Currentl” text followed by the link of the file. I guess I have to customize the class ClearableFileInput but what I’ve triid so far does not work.
The field definition in the “Param” Model:
UW_logo = models.ImageField("Logo:", upload_to='logos/', null=True, blank=True)
My form:
class CustomClearableFileInput(ClearableFileInput):
def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
context["widget"]["initial_text"] = "Texte initial personnalisé"
return context
class ParametersForm(ModelForm):
class Meta:
model= Param
#fields = '__all__'
exclude = ['UW_Sirene_API_consumer_key','UW_Sirene_API_consumer_secret',
'UW_Sirene_API_access_token', 'UW_Sirene_API_access_token_expiration_date',
'UW_ms_drive_id','UW_ms_site_id',
]
widgets = {'UW_logo': CustomClearableFileInput(attrs={'class': 'form-control-file'})},
#widgets = {'UW_logo': forms.ClearableFileInput(attrs={'class': 'form-control-file'})}
labels = {'UW_logo': "Logo actuel"}
The html piece of code:
<div class=" col-md-1 d-flex align-items-center">
<label for="{{form.UW_logo.id_for_label}}" class="mx-2 text-secondary">{{form.UW_logo.label}}</label>
</div>
<div class="col-md-6 d-flex align-items-center">
<div class="position-relative ">
{% if form.instance.UW_logo %}
<img src="{{ form.instance.UW_logo.url }}" alt="Logo" class="logo-img">
{% else %}
Aucun
{% endif %}
</div>
{{form.UW_logo}}
<div class="fieldWrapper">{{form.UW_logo.errors}}</div>
</div>
I also tried the following for my CustomClearableFileInput:
class CustomClearableFileInput(ClearableFileInput):
initial_text = "Texte initial personnalisé"
I alway get the “Currently” text followed by the link.
Any help would be appreciated to get rid of this text and the link (as you may have notice in the html, I displayed the logo, if any)
Richard