Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'InMemoryUploadedFile' object is not subscriptable

Tags:

python

django

I have a Google Appengine Project using Python2.7 and Django1.2 on Eclipse, that allows the user to use a form to upload a picture, resize it, and store it as a BLOB field.

I added a breakpoint where I indicated below, and saw "file['content']" showing a value " TypeError: 'InMemoryUploadedFile' object is not subscriptable" in the Expressions view.

When I step into or over this line, it jumps to the error handler.

Can someone please advise how I can fix this problem? Thanks in advance!

if req.method == 'POST':
    try:
        u_form = UserInfoForm(req.POST)
        if not u_form.is_valid():
            return err_page(_('Error'))

        u = coffeeuser.CoffeeUser.all().filter('user =', user_info).get()
        u.nickname = user_info.nickname()
        u.realname = req.POST.get('real_name')
        u.phone = req.POST.get('phone')
        u.address = req.POST.get('address')
        if req.FILES.get('photo_file'):                
            file = req.FILES.get('photo_file')
            img = images.Image(file['content'])   <<<Breakpoint...Error occurs here
            img.resize(width=50, height=50)
            resized_img = img.execute_transforms(output_encoding=images.JPEG)
            u.photo_file = db.Blob(resized_img)
        u.put()
        return HttpResponseRedirect('/user/')
    except Exception, x:
        return err_page(_('Error'))

And here is the dump of the Console window as this happens. I don't see any error messages here.

INFO 2012-05-26 07:34:21,114 dev_appserver.py:2891] "GET /favicon.ico HTTP/1.1" 404 - DEBUG 2012-05-26 07:35:15,960 dev_appserver.py:656] Matched "/user/" to CGI dispatcher with path main.py DEBUG 2012-05-26 07:35:16,319 dev_appserver_import_hook.py:1246] Enabling PIL: ['_imaging', '_imagingcms', '_imagingft', '_imagingmath'] DEBUG 2012-05-26 07:35:16,322 dev_appserver_import_hook.py:1246] Enabling django: [] DEBUG 2012-05-26 07:35:16,322 dev_appserver.py:1624] Executing CGI with env: {'HTTP_REFERER': 'http://localhost:8080/user/', 'REQUEST_ID_HASH': 'C1DFD96E', 'SERVER_SOFTWARE': 'Development/1.0', 'SCRIPT_NAME': '', 'REQUEST_METHOD': 'POST', 'PATH_INFO': '/user/', 'HTTP_ORIGIN': 'http://localhost:8080', 'SERVER_PROTOCOL': 'HTTP/1.0', 'QUERY_STRING': '', 'CONTENT_LENGTH': '927730', 'HTTP_ACCEPT_CHARSET': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'APPENGINE_RUNTIME': 'python27', 'TZ': 'UTC', 'HTTP_COOKIE': 'RememberMe=YPD/ztDwsHCs3J9cPG5c+g==; dev_appserver_login="[email protected]:False:185804764220139124118"; sessionid=2b5fc41e4c0332b1161a002ae12e616b; csrftoken=05e24dcb62093082dc1fafe66c0a6dbb', 'SERVER_NAME': 'localhost', 'REMOTE_ADDR': '127.0.0.1', 'SDK_VERSION': '1.6.5', 'PATH_TRANSLATED': 'C:\_dev\eclipse-work\gae\pydev5\src\main.py', 'SERVER_PORT': '8080', 'CONTENT_TYPE': 'multipart/form-data; boundary=----WebKitFormBoundaryC4BfGc98AzYhJTQD', 'CURRENT_VERSION_ID': '1.1', 'USER_ORGANIZATION': '', 'USER_ID': '185804764220139124118', 'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19', 'HTTP_HOST': 'localhost:8080', 'HTTP_CONNECTION': 'keep-alive', 'HTTP_CACHE_CONTROL': 'max-age=0', 'USER_EMAIL': '[email protected]', 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8', 'APPLICATION_ID': 'dev~quizoncloud', 'GATEWAY_INTERFACE': 'CGI/1.1', 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8', 'AUTH_DOMAIN': 'gmail.com', '_AH_ENCODED_SCRIPT_NAME': '/user/'} DEBUG 2012-05-26 07:36:19,815 datastore_stub_index.py:181] No need to update index.yaml

like image 337
Jason O. Avatar asked Dec 06 '22 14:12

Jason O.


1 Answers

file is not a dictionary, so you can't do key lookup like that. Perhaps you mean file.content?

Although I don't think the object has a content attribute either - see the documentation for UploadedFile objects. Maybe you meant file.read()?

(Also, don't call your variable file - that hides the built-in file function).

like image 150
Daniel Roseman Avatar answered Dec 31 '22 15:12

Daniel Roseman