Django: after form.is_valid check, cleaned_data is missing the image field

I have a form, with a bunch of fields, and then I have a:

    profile_image=forms.ImageField(required=False)

The problem si that after the form.is_valid() check,

form.cleaned_data.get('first_name')

for example DOES return the actual name, however,

form.cleaned_data.get('profile_image')

DOES NOT return anynthing.

In the print(request.POST) output, i get

u'profile_image': [u'02 Portfolio Page.jpg']

but in the print(form.cleaned_data) i get:

 'profile_image': None

Why is the file lost at the is_valid check? What should I do?

UPDATE:

class NewChickForm(forms.Form):
first_name = forms.CharField()
last_name = forms.CharField()
profile_image=forms.ImageField(required=False)
def do_save(self):
    u = Subject(
        first_name = self.cleaned_data.get('first_name'),
        last_name = self.cleaned_data.get('last_name'),
        profile_image = self.cleaned_data.get('profile_image'),
    )
    print(self.cleaned_data)
    u.save()
    return u

and

s = Subject()
form = NewChickForm(request.POST) # 1)do i add here `request.FILES` ?
if form.is_valid():
    s = form.do_save()
            # 2) s.profile_image = form.cleaned_data.get('profile_image')?

Even if i do #1) and #2), i still get NONE

Answers


Make sure you've used the correct enctype on your HTML form:

<form enctype="multipart/form-data" method="post" action="/foo/">

Also, make sure you bind request.FILES when you construct the form object.

# Bound form with an image field, data from the request
>>> f = ContactFormWithMugshot(request.POST, request.FILES)

Binding uploaded files to a form

Edit: File Uploads


Need Your Help

Accessing parent objects while using a javascript module pattern variant

javascript class object design-patterns parent

Hi I'm using this module pattern variant and I'm looking for the best way to get access to a parent object. I realise there is no way to know what parents an object has so I want to include some co...