formset is valid but returns empty cleaned_data field

2.5k Views Asked by At

I have a formset that returns empty cleaned_data, even though the formset is valid. my view is like this:

def edit_files(request):
    file_formSet = formset_factory(FileUploadForm)
    if request.POST:
        formset = file_formSet(request.POST, request.FILES)
        if formset.is_valid():
            for form in formset:
                form_data = form.cleaned_data
                if form_data:
                    up_file = upload_file(form_data['file'])
    else:
        formset = file_formSet()

    return render_to_response('edit_files.html', {'formset': formset})

it is an upload form and with this problem I can't save files upload_file is one of my implemented methods and it works fine, I have tested this.

5

There are 5 best solutions below

0
On BEST ANSWER

ok, i just wanna complete the topic so if anyone has the same problem, he/she can use this page to solve it main keys to this solution are: 1- never forget the encryption type 2- use separate forms for different functionality

so in the template i should use:

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

and use two separate forms for add and delete files like this: this one for adding files:

<form method="POST" action="." encrypt="multipart/form-data" id="add_file">

and this one for deleting files:

<form method="POST" action="." encrypt="multipart/form-data" id="delete_file">
1
On

I think you need to change the following line;

... snip ... 

for form in formset.forms:
    form_data = form.cleaned_data

... snip ...

You appear to be missing the .forms part from the formset.

0
On

ok i found out after using some break points, that it's actually request's fault, request.FILES is empty

0
On

tnx for all of your answers, i'v solved the problem, i had one form with two functionality: add_file and del_file after i separated them, every thing was ok but there is a problem in the looking of the page after i add some files, like 3 file and then request.POST sent and i return to the page again i actually see three row of file input, instead of one it really reduce the beauty any advice?

0
On

I ran into the same error message when I forgot to append the parenthesis () after is_valid. Formset.is_valid returned True, but there was no cleaned data simply because the is_valid() function was not called to return the cleaned data.