validation - Django: after form.is_valid check, cleaned_data is missing the image field -
i have form, bunch of fields, , have a:
profile_image=forms.imagefield(required=false)
the problem si after form.is_valid() check,
form.cleaned_data.get('first_name')
for example return actual name, however,
form.cleaned_data.get('profile_image')
does not return anynthing.
in print(request.post) output,
u'profile_image': [u'02 portfolio page.jpg']
but in print(form.cleaned_data) get:
'profile_image': none
why file lost @ is_valid check? should 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 add here `request.files` ? if form.is_valid(): s = form.do_save() # 2) s.profile_image = form.cleaned_data.get('profile_image')?
even if #1) , #2), still none
make sure you've used correct enctype
on html form:
<form enctype="multipart/form-data" method="post" action="/foo/">
also, make sure bind request.files
when construct form object.
# bound form image field, data request >>> f = contactformwithmugshot(request.post, request.files)
edit: file uploads
Comments
Post a Comment