0
votes

Multiple files do not save in admin, only the first saved in admin.

 class Image(models.Model):
        imageuploader_profile = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE, null=True, blank=True)
        image = models.FileField(upload_to ='pictsagram/')
        image_caption = models.CharField(max_length=700)
    
def upload(request):
    form = PostForm(request.POST,request.FILES)
    if request.method == "POST":
        if form.is_valid():
            for f in request.FILES.getlist('image'):
                post = Image(image=f)
                post = form.save(commit=False)
                post.imageuploader_profile = request.user
                print(request.user)
                post.save()
                form.save()
                return redirect('/')
        else:
            form = PostForm()
    return render(request, 'upload.html', {"form": form})

class PostForm(forms.ModelForm):
    class Meta:
    model = Image
    fields = ('image_caption', 'image',)

<form action="{% url 'site:upload' %}" method="post" enctype="multipart/form-data">
{%csrf_token%}
<input type="file" name="file" multiple onchange="loadFile(event)" required="" id="id_file">
<button type="submit">Post</button>
</form>
2
Remove the return redirect('/') out of the for f in request.FILES.getlist('image'): loop. You're redirecting before saving the rest of the files.Ben
@Ben I did as you said, when I upload two files only the last file is saved. I want the both file to be save and display in my databaseHi3e
You may need to move the form.save() line out of the for loop as well (but keep form.save() in the if form.is_valid() section.Ben
@Ben I have done that and still yet it saves only one file.Hi3e
I have still not solved this, anyone can?Hi3e

2 Answers

0
votes

It looks like your form is for an Image object, but you're trying to create multiple images from a single form submit.

You are creating the Image(), but it doesn't look like you're attaching it to the form. So you'll probably need to reorganize the view something like:

def upload(request):
    if request.method == "POST":
        for image_file in request.FILES.getlist('image'):
            form = PostForm(request.POST, image_file)
            if form.is_valid():
                image = form.save(commit=False)
                image.imageuploader_profile = request.user
                image.save()
                form.save()
        return redirect('/')
    else:
        form = PostForm()
    return render(request, 'upload.html', {"form": form})

Also, cut/paste can mess up formatting, but always double-check your indentation for intended flow.

0
votes

use this code:-----------------------------

 def upload(request):
        form = PostForm(request.POST,request.FILES)
        if request.method == "POST":
            if form.is_valid():
                for f in request.FILES.getlist('file'):
                    post = Image(image=f)
                    post = form.save(commit=False)
                    post.imageuploader_profile = request.user
                    print(request.user)
                    post.save()
                    form.save()
            return redirect('/')
            else:
                form = PostForm()
        return render(request, 'upload.html', {"form": form})

    class PostForm(forms.ModelForm):
        class Meta:
        model = Image
        fields = ('image_caption', 'image',)

    <form action="{% url 'site:upload' %}" method="post" enctype="multipart/form-data">
    {%csrf_token%}
    <input type="file" name="file" multiple onchange="loadFile(event)" required="" id="id_file">
    <button type="submit">Post</button>
    </form>