Matias Mir
Matias Mir

Reputation: 45

Django can't upload multiple files and request.FILES seems empty

I'm using django 2.0 and python 3.6, I've tried almost everything to get the values from request.FILES but nothing seems to work, I think that I'm missing something very simple but I can't find it.

models.py

class Imagen(models.Model):
imagen = models.FileField(max_length=200, blank=True, upload_to='%Y/%m/%d/')

views.py

def prop_add(request):
  if request.method == 'POST':
    propiedad_instance = Propiedad(tipo = request.POST.get('tipo'), tamano=request.POST.get('tamano'), habitaciones = request.POST.get('habitaciones'), banos = request.POST.get('banos'), descripcion= request.POST.get('descripcion'), direccion= request.POST.GET('descripcion'), precio= request.POST.get('precio'), ubicacion= Ubicacion.objects.get(barrio= request.POST.get('barrio')))  
    propiedad_intance.save()
    for filename in request.FILES.iteritems():
        name = request.FILES[filename].name
        print('name =' + name)  ## <-- not printing anything
        print('file = ' +  file)## <-- not printing anything
        print('filename = ' + filename)## <-- not printing anything
  ubicaciones = Ubicacion.objects.all()
  ctx = {'ubicaciones': ubicaciones}
  return render(request, 'main/add_modal.html', ctx)

HTML template

<form method='post' action=''  enctype="multipart/form-data">
   <input name='imagen' type="file" multiple/>
   <button type='submit' class="btn waves-effect">Upload</button>
</form>

that views.py is the one that I have right now but so far I've tried all the following variants:

**1**
if request.method == 'POST':
  for f in request.FILES.getlist('imagen'):
    filename = f.name
    print(filename) ## <-- not printing anything

**2**
if request.method== 'POST':
  form = FileUploadForm(request.POST, request.FILES) ## form imported from forms.py
  if form.is_valid():
     print('form is valid!') ## <-- not printing anything
  else:
     print('form not valid ') ## <-- not printing anything either!! IDK WHY
**3**
if request.method == 'POST':
  print('request.method = "POST" checked ') # <-- Not priting anything! but the model below is being saved to the database! my brain is about to explode right now haha.
  propiedad_instance = Propiedad(tipo = request.POST.get('tipo'), tamano=request.POST.get('tamano'), habitaciones = request.POST.get('habitaciones'), banos = request.POST.get('banos'), descripcion= request.POST.get('descripcion'), direccion= request.POST.GET('descripcion'), precio= request.POST.get('precio'), ubicacion= Ubicacion.objects.get(barrio= request.POST.get('barrio')))    
  propiedad_intance.save()
  files = request.FILES.getlist('imagen')
    if files:
        for f in files:
            print('something') #<-- not printing anything
            print(f) #<-- not printing anything
            print(f.name) <-- not printing anything
    else:
        print('nothing here') #<--- not printing anything   

Console log after submitting the form enter image description here

Upvotes: 2

Views: 3567

Answers (1)

Angela
Angela

Reputation: 1731

The correct way to upload multiple files as per the documentation would be..

files = request.FILES.getlist('file_field') #'file_field' --> 'imagen' for you
if form.is_valid():
        for f in files:
            ...  # Do something with each file.

Your html looks ok. Replace your view code as per the above and check if its making to the POST in the view?

Django Docs - Multiple File Upload

Upvotes: 2

Related Questions