[Answer]-Multiple filename in django formset

0👍

Instead i have 2 for, i do everything in only one for.

for file in request.FILES.getlist('form-0-source'):
    file_type = file.content_type
    if file_type == "image/png" or file_type == "image/jpeg" or file_type == "image/gif":
        name= file.name.split('.')[0]
        newmedia = Media(source=file, filename=name, content_type = "photos", created='03/25/12')
        newmedia.save()                  
        image2 = ImageComponent(page=Page_key, order=0, label="imagem", x=0, y=0, width=0, height=0, viewport_type="simle_page", keywords="imagem", media=newmedia, is_slideshow='true')
        image2.save()
        slide.image.add(image2)                 
    else:
        return render_to_response('revista_digital/error.html', context_instance=RequestContext(request))

1👍

I guess you omitted a tabulation :

    for filename, file in request.FILES.iteritems():
        name = request.FILES[filename].name               
        for form in formset.forms:        
            file_type = file.content_type
            if file_type == "image/png" or file_type == "image/jpeg" or file_type == "image/gif":
                newmedia = formset.save(commit=False)
                newmedia.filename = name
                newmedia.content_type = "photos"
                newmedia.save()

otherwise it keeps always the name of the last file you processed.

EDIT : in fact, I guess this isn’t what you want either. You should keep track of the formset.forms you already processed and then assign the name that goes along with a formset.forms that hasn’t been processed.

👤Zashas

Leave a comment