0

I'm trying to upload multiple file using django but my code is uploadign only one image not all images, I'm trying to upload multiple images without using any pluging or javascript just a simple way to do a foreach because I want to use only one input field has multiple attribute like in PHP codes

My model is :

from django.db import models
from django.contrib.auth.models import User
from album.models import Album
from django.core.files import File

class Images(models.Model):
    user = models.ForeignKey(User, on_delete= models.CASCADE)
    album = models.ForeignKey(Album, on_delete= models.CASCADE)
    
    image_value = models.FileField(upload_to='galleries/images', max_length=350, blank=True, null=True)
    
    created_on = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'album_images'

    def __str__(self):
        return self.album

Form is :

from django import forms
from django.forms import ClearableFileInput
from .models import *

class ImagesUploadClass(forms.ModelForm):
    class Meta:
        model = Images
        
        exclude = ['user']
        fields = ('image_value')

View is :

from django.shortcuts import render, redirect
from .forms import ImagesUploadClass
from .models import Images
from django.http import HttpResponseRedirect

def uploadMultiPleImagesFunction(request):
    upload_image = ImagesUploadClass()
    
    if request.method == "POST":
        form_add_data = ImagesUploadClass(request.POST, request.FILES)

        if (form_add_data.is_valid()):
            form_add_data.save.save()
            return HttpResponseRedirect('/album')
        else:
            print(form_add_data.errors)

    return render(request, 'images/upload_images.html', {
        'upload_image' : upload_image,
        })

And my template html is :

{% extends "base.html" %}

{% block maincontent %}

    <form method="POST" action="" enctype="multipart/form-data">

        {% csrf_token %}

        <input type="hidden" name="{{ upload_image.user.name }}" value="{{ request.user.id }}">

        <label>Title</label>
        <input type="text" name="{{ upload_image.album.name }}" placeholder="" autocomplete="off" {% if upload_image.album.value != None %} value="{{ upload_image.album.value }}" {% endif %}/>

        <label>Images</label>
        <input type="file" name="{{ upload_image.image_value.name }}" placeholder="" {% if upload_image.image_value.value != None %} value="{{ upload_image.image_value.value }}" {% endif %} multiple />

        <button type="submit">UPLOAD</button>

    </form>

{% endblock %}
im-learning
  • 117
  • 2
  • 10
  • Does this answer your question? [how to upload multiple images to a blog post in django](https://stackoverflow.com/questions/34006994/how-to-upload-multiple-images-to-a-blog-post-in-django) – iklinac Oct 19 '20 at 04:22
  • @iklinac that meathod I see tey use image and post at the same time I'm looking for sothing easy only for images – im-learning Oct 19 '20 at 04:34
  • @iklinac I did find solution and I update my question I add solution – im-learning Oct 19 '20 at 09:31
  • answer your own question instead, if you found it answer on stack overflow close it as duplicate linking to that question – iklinac Oct 19 '20 at 09:34

1 Answers1

0

I FOUND SOLUTION

model :

from django.db import models
from django.contrib.auth.models import User
from album.models import Album
from django.core.files import File

class Images(models.Model):
    user = models.ForeignKey(User, on_delete= models.CASCADE)
    title = models.CharField(max_length=350, blank=True, null=True)
    
    image_value = models.FileField(upload_to='galleries/images', max_length=350, blank=True, null=True)
    
    created_on = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'album_images'

    def __str__(self):
        return self.title

in form.py

from django import forms
from django.forms import ClearableFileInput
from .models import *

class ImagesUploadClass(forms.ModelForm):
    
    class Meta:
        model = Images
        fields = ('user', 'title', 'image_value', )

In views.py

from django.shortcuts import render, redirect
from django.http import HttpResponseRedirect
from .forms import ImagesUploadClass
from .models import Images

def uploadMultiPleImagesFunction(request):

    upload_image = ImagesUploadClass()
    
    if request.method == "POST":
        
        form_add_data = ImagesUploadClass(request.POST, request.FILES)
        
        files = request.FILES.getlist('image_value')
        
        if (form_add_data.is_valid()):
            
            for image in files:

                Images.objects.create(user_id = request.user.id, title=request.POST['title'], image_value = image)

            return HttpResponseRedirect('/')

        else:
            print(form_add_data.errors)

    return render(request, 'images/upload_images.html', {
        'upload_image': upload_image
        })

Templates :

{% extends "base.html" %}

{% block maincontent %}

    <form method="POST" action="" enctype="multipart/form-data">

        {% csrf_token %}

        <input type="hidden" name="{{ upload_image.user.name }}" value="{{ request.user.id }}">

        <label>Title</label>
        <input type="text" name="{{ upload_image.title.name }}" placeholder="" autocomplete="off" {% if upload_image.title.value != None %} value="{{ upload_image.title.value }}" {% endif %}/>

        <label>Images</label>
        <input type="file" name="{{ upload_image.image_value.name }}" placeholder="" {% if upload_image.image_value.value != None %} value="{{ upload_image.image_value.value }}" {% endif %} multiple />

        <button type="submit">UPLOAD</button>

    </form>

{% endblock %}
im-learning
  • 117
  • 2
  • 10