blob: ec1bd1d6495f7a803b67e21cfdec2ff8284079a3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
import os
from django.contrib.admin.widgets import AdminFileWidget
from django.utils.safestring import mark_safe
from django.conf import settings
def thumbnail(image_path):
absolute_url = os.path.join(settings.IMAGES_URL, image_path[7:])
print(absolute_url)
return '<img style="max-width: 400px" src="%s" alt="%s" />' % (absolute_url, image_path)
class AdminImageWidget(AdminFileWidget):
"""
A FileField Widget that displays an image instead of a file path
if the current file is an image.
"""
def render(self, name, value, attrs=None):
output = []
file_name = str(value)
if file_name:
file_path = '%s' % (file_name)
output.append('<a target="_blank" href="%s">%s</a>' % (file_path, thumbnail(file_name)))
output.append(super(AdminFileWidget, self).render(name, value, attrs))
return mark_safe(''.join(output))
|