feat: Add preview widget in Image Field Form #4

Merged
root merged 1 commits from feat-image-field-preview into master 2023-11-29 17:37:07 +01:00
4 changed files with 18 additions and 2 deletions

View File

@ -20,3 +20,5 @@ MEDIA = getattr(settings, 'GAS_MEDIA', {
}) })
EXTRA_MEDIA = getattr(settings, 'GAS_EXTRA_MEDIA', None) EXTRA_MEDIA = getattr(settings, 'GAS_EXTRA_MEDIA', None)
IMAGE_PREVIEW_WIDTH = getattr(settings, 'IMAGE_PREVIEW_WIDTH', 240)

View File

@ -262,3 +262,7 @@ label {
width: 20em; width: 20em;
padding: 1em; padding: 1em;
} }
.preview {
display: block;
}

View File

@ -0,0 +1,5 @@
{% if widget.is_initial %}{{ widget.initial_text }}: <a href="{{ widget.value.url }}"><img src="{{ widget.value.url }}" width="{{ widget.attrs.IMAGE_PREVIEW_WIDTH }}" class="preview" /></a>{% if not widget.required %}
<input type="checkbox" name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}"{% if widget.attrs.disabled %} disabled{% endif %}>
<label for="{{ widget.checkbox_id }}">{{ widget.clear_checkbox_label }}</label>{% endif %}<br>
{{ widget.input_text }}:{% endif %}
<input type="{{ widget.type }}" name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>

View File

@ -1,7 +1,8 @@
from django import forms from django import forms, template
from django import template
from django.template.loader import render_to_string from django.template.loader import render_to_string
from ..gas_settings import IMAGE_PREVIEW_WIDTH
register = template.Library() register = template.Library()
@ -19,6 +20,10 @@ def base_form_field(field, css=None, container_class='', add_another_url=None, f
elif isinstance(field.field, forms.DateField): elif isinstance(field.field, forms.DateField):
field.field.widget.input_type = 'date' field.field.widget.input_type = 'date'
if isinstance(field.field, forms.ImageField):
field.field.widget.template_name = 'gas/tags/widgets/image_preview.html'
field.field.widget.attrs['IMAGE_PREVIEW_WIDTH'] = IMAGE_PREVIEW_WIDTH
if isinstance(field.field, forms.SplitDateTimeField): if isinstance(field.field, forms.SplitDateTimeField):
field.field.widget.widgets[1].attrs['placeholder'] = '00:00:00' field.field.widget.widgets[1].attrs['placeholder'] = '00:00:00'
else: else: