[Django]-How to save each file in request.FILES with for loop in Django?

4👍

Managed to solve my problem by creating an instance of BookForm and submitting the data through the loop. Hope this helps somebody else in the future.

Views.py

    def upload_book(request):
if request.method == 'POST':
    for f in request.FILES.getlist('pdf'):
        form = BookForm(request.POST, request.FILES)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.pdf = f
            obj.save()
    return redirect('book_list')
else:
    form = BookForm()
return render(request, 'upload_book.html', {
    'form': form
})
👤Elmi

Leave a comment