[Python] uploading multiple images in Django admin

Discussão em 'Python' iniciado por Stack, Março 29, 2025.

  1. Stack

    Stack Membro Participativo

    I have a category and for each category I want to add multiple images but whatever I do, I have just one upload file but I want to have multiple. In my Django project I have this class:

    in my model file:

    class SalonSampleImages(models.Model):
    service = models.ForeignKey(Salon, on_delete=models.CASCADE)
    Category = models.ForeignKey(
    ServiceCategory,
    on_delete=models.SET_NULL,
    null=True,
    blank=True,
    verbose_name="the main service",
    default=1
    )
    image = models.ImageField(upload_to='salon_images/', verbose_name="images")

    def __str__(self):
    return f"images"

    class Meta:
    verbose_name = "picture"
    verbose_name_plural = "pictures"


    in my admin file:

    class SalonSampleImagesInLineFormSet(forms.BaseInlineFormSet):
    def clean(self):
    super().clean()
    total_images = len([form for form in self.forms if form.cleaned_data and not form.cleaned_data.get('DELETE', False)])
    if total_images < 3:
    raise ValidationError("you must all at least 3 images")
    if total_images > 10:
    raise ValidationError("you cannot add more than 10 images")

    class SalonSampleImagesInLine(admin.TabularInline):
    model = SalonSampleImages
    formset = SalonSampleImagesInLineFormSet
    extra = 1


    and I registered all

    then I created a model file:

    class MultiFileInput(forms.ClearableFileInput):
    allow_multiple_selected = True

    def __init__(self, attrs=None):
    if attrs is None:
    attrs = {}
    attrs.setdefault('multiple', 'multiple')
    super().__init__(attrs=attrs)

    def value_from_datadict(self, data, files, name):
    return files.getlist(name)

    class MultiImageUploadForm(forms.Form):
    category = forms.ModelChoiceField(
    queryset=ServiceCategory.objects.all(),
    label="choose category"
    )
    images = forms.FileField(
    widget=MultiFileInput(),
    label="upload pictures"
    )


    then I created a html file:

    {% extends "admin/base_site.html" %}

    {% block content %}
    <h1>{{ title }}</h1>
    <form method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="save the image" class="default">
    </form>
    {% endblock %}


    and my settings.py:

    TEMPLATES = [
    {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [BASE_DIR / 'templates'],
    'APP_DIRS': True,
    'OPTIONS': {
    'context_processors': [
    'django.template.context_processors.debug',
    'django.template.context_processors.request',
    'django.contrib.auth.context_processors.auth',
    'django.contrib.messages.context_processors.messages',
    ],
    },
    },
    ]

    Continue reading...

Compartilhe esta Página