TypeError:'InMemoryUploadedFile'对象不可订阅

时间:2012-05-26 14:47:24

标签: python django

我在Eclipse上使用Python2.7和Django1.2进行Google Appengine项目,允许用户使用表单上传图片,调整图片大小并将其存储为BLOB字段。

我添加了一个断点,我在下面指出,并在“表达式”视图中看到“file ['content']”显示值“TypeError:'InMemoryUploadedFile'对象不可订阅”。

当我进入或超过此行时,它会跳转到错误处理程序。

有人可以建议我如何解决这个问题吗?提前谢谢!

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'))

这是发生这种情况时控制台窗口的转储。我在这里看不到任何错误消息。

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]将“/ user /”与路径main.py匹配到CGI调度程序 DEBUG 2012-05-26 07:35:16,319 dev_appserver_import_hook.py:1246]启用PIL:['_imaging','_ imagingcms','_imagingft','_ imagingmath'] DEBUG 2012-05-26 07:35:16,322 dev_appserver_import_hook.py:1246]启用django:[] DEBUG 2012-05-26 07:35:16,322 dev_appserver.py:1624]用env执行CGI: {'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 +克==​​; dev_appserver_login = “test@example.com:错误: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,与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 ':'test@example.com','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]无需更新index.yaml

2 个答案:

答案 0 :(得分:10)

file不是字典,所以你不能像这样进行密钥查找。也许你的意思是file.content

虽然我认为该对象也不具有content属性 - 请参阅the documentation了解UploadedFile对象。也许你的意思是file.read()

(另外,请勿调用变量file - 隐藏内置file函数)。

答案 1 :(得分:0)

我建议你在产生异常的行之前添加[[print'DEBUG:&gt;',file,type(file)]]行。此错误是关于“文件”对象不是映射类型。