From 3ed2a3da11228913465226eee9137c93fc3c5374 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Tue, 24 Apr 2018 11:37:26 +0200 Subject: Forms: preview posted images --- ishtar_common/widgets.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'ishtar_common/widgets.py') diff --git a/ishtar_common/widgets.py b/ishtar_common/widgets.py index 60e5fc016..192de6f65 100644 --- a/ishtar_common/widgets.py +++ b/ishtar_common/widgets.py @@ -25,6 +25,7 @@ from django import forms from django.conf import settings from django.core.urlresolvers import reverse, NoReverseMatch from django.db.models import fields +from django.db.models.fields.files import ImageFieldFile from django.forms import ClearableFileInput from django.forms.utils import flatatt from django.forms.widgets import CheckboxSelectMultiple as \ @@ -294,6 +295,17 @@ class ImageFileInput(ClearableFileInput): template_name = 'widgets/image_input.html' NO_FORM_CONTROL = True + def format_value(self, value): + if self.is_initial(value): + return value + # try to display posted images + if hasattr(value, 'file'): + full_path = unicode(value.file) + if full_path.startswith(settings.MEDIA_ROOT): + value.url = settings.MEDIA_URL + full_path[ + len(settings.MEDIA_ROOT):] + return value + class SquareMeterWidget(forms.TextInput): def render(self, name, value, attrs=None, renderer=None): -- cgit v1.2.3