I have a model like this
class Participation(models.Model):
# ... some attributes ...
thumbnail = models.ImageField(
verbose_name='immagine di copertina',
blank=True,
null=True,
)
and I add the following code to show a thumbnail in the admin page:
class AdminImageWidget(ClearableFileInput):
"""A ImageField Widget for admin that shows a thumbnail"""
def render(self, name, value, attrs=None):
output = []
if value and getattr(value, 'url', None):
image_url = value.url
file_name = str(value)
output.append(
u'%s '
u'<a href="%s" target="_blank">'
u'<img src="%s" alt="%s" style="max-width: 100px; max-height: 100px; border-radius: 5px;" />'
u'</a><br/><br/>%s '
% (_('Currently:'), image_url, image_url, file_name, _('Change:'))
)
output.append(super(ClearableFileInput, self).render(name, value, attrs))
return mark_safe(u''.join(output))
Even if it worked fine for the thumbnail, the super(ClearableFileInput, self).render(name, value, attrs)
calling is adding just the browse button, but it isn't showing the clear checkbox, so I cannot delete the selected thumbnail.
What's wrong with this code? How can I add the checkbox mantaining the thumbnail too?
I solved my problem recoding the
AdminImageWidget
as following:Note the:
that make possible to show the thumbnail.