image KeyError

15 views
Skip to first unread message

psychok7

unread,
Apr 21, 2012, 4:30:20 PM4/21/12
to django...@googlegroups.com
hi there , so i am not sure if i am doing this the right way, but im successfully able to upload files(images) and store them in my defined folder  and the link in my mysql  database.
 the error comes when i try to return the image with the HTTP RESPONSE were i get a image keyError.. i am not sure wht i am doing wrong.. 
here is my code:

def handle_uploaded_file(f,u):
    profile=u.get_profile()
    profile.avatar=f.name
    destination = open('images/users/'+f.name, 'wb+')
    for chunk in f.chunks():
        destination.write(chunk)
    destination.close()
    profile.save()

def upload_file(request,user_id):
    u = get_object_or_404(User, pk=user_id)
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES['file'],u)   
            uploadedImage = form.cleaned_data['image'] 
            
            return HttpResponse(uploadedImage.content,mimetype="image/jpeg")
    else:
        form = UploadFileForm()

    c = {'form': form,'user' : u}
    c.update(csrf(request))
    return render_to_response('accounts/upload.html', c) 

<form enctype="multipart/form-data" method="post" action="/accounts/{{ user.id }}/upload/">
   {% csrf_token %}   
   <table>
       {{form.as_table}}
    </table>
    <input type="submit" value="Submit" id="Save"/>
</form>

thanks in advance

akaariai

unread,
Apr 21, 2012, 6:14:12 PM4/21/12
to Django users
Your question is very hard to answer without the full error message.
Some wild guesses of what is going wrong: maybe the
cleaned_data['image'] is missing or it isn't a file?

- Anssi

psychok7

unread,
Apr 21, 2012, 6:16:15 PM4/21/12
to django...@googlegroups.com
ok i was copy pasting from example on the internet.. where do i check if it exists or not?
Reply all
Reply to author
Forward
0 new messages