收到 TypeError,“不可索引”在 webapp2.RequestHandler 的 post 方法中执行 if self.request.POST['file'] 时

发布于 2024-11-04 15:48:36 字数 3615 浏览 6 评论 0原文

我正在尝试使用此代码上传文件:

<form id="form1" action="convert" enctype="multipart/form-data" method="post">
    <input type="file" name="file"/>
    <div><input id="submit_button" type="submit" value="Upload"/></div>
</form>

class Convert(RequestHandler):
    @login_required
    def post(self):
        session = Session(writer="cookie", session_expire_time = 3600, set_cookie_expires = True)
        if session['id']:
            file = self.request.POST['file']
            if file and file.type and file.value:

但我不断收到此错误:

if file and file.type and file.value:
  File "C:\Python25\lib\cgi.py", line 633, in __len__
    return len(self.keys())
  File "C:\Python25\lib\cgi.py", line 609, in keys
    raise TypeError, "not indexable"

有趣的是,这曾经有效!这里出了什么问题?请注意,我使用的是 webapp2。 在此处输入图像描述

以及 页面显示:FieldStorage 实例可以像 Python 字典一样进行索引。它允许使用 in 运算符进行成员资格测试,并且还支持标准字典方法 keys() 和内置函数 len()

完整堆栈跟踪:

Traceback (most recent call last):
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3858, in _HandleRequest
    self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3792, in _Dispatch
    base_env_dict=env_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 580, in Dispatch
    base_env_dict=base_env_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2918, in Dispatch
    self._module_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2822, in ExecuteCGI
    reset_modules = exec_script(handler_path, cgi_path, hook)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2704, in ExecuteOrImportScript
    script_module.main()
  File "C:\myproject\main.py", line 16, in main
    run_wsgi_app(application)
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\util.py", line 98, in run_wsgi_app
    run_bare_wsgi_app(add_wsgi_middleware(application))
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\util.py", line 116, in run_bare_wsgi_app
    result = application(env, _start_response)
  File "C:\myproject\webapp2\__init__.py", line 1053, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\myproject\webapp2\__init__.py", line 1098, in wsgi_app
    self.handle_exception(request, response, e)
  File "C:\myproject\webapp2\__init__.py", line 1092, in wsgi_app
    self.router.dispatch(self, request, response, match)
  File "C:\myproject\webapp2\__init__.py", line 949, in dispatch
    handler.handle_exception(e, app.debug)
  File "C:\myproject\webapp2\__init__.py", line 942, in dispatch
    getattr(handler, method)(*args)
  File "C:\myproject\py\decorators.py", line 15, in decorated
    return _login_required (self) or func(self, *args, **kwargs)
  File "C:\myproject\py\document.py", line 42, in post
    if file and file.type and file.value:
  File "C:\Python25\lib\cgi.py", line 633, in __len__
    return len(self.keys())
  File "C:\Python25\lib\cgi.py", line 609, in keys
    raise TypeError, "not indexable"
TypeError: not indexable

我修复它的方法是撕掉 if file:并使用 python has_key 方法检查 self.request.POST 是否具有密钥“文件”

I am trying to upload a file using this code:

<form id="form1" action="convert" enctype="multipart/form-data" method="post">
    <input type="file" name="file"/>
    <div><input id="submit_button" type="submit" value="Upload"/></div>
</form>

class Convert(RequestHandler):
    @login_required
    def post(self):
        session = Session(writer="cookie", session_expire_time = 3600, set_cookie_expires = True)
        if session['id']:
            file = self.request.POST['file']
            if file and file.type and file.value:

but I keep on getting this error:

if file and file.type and file.value:
  File "C:\Python25\lib\cgi.py", line 633, in __len__
    return len(self.keys())
  File "C:\Python25\lib\cgi.py", line 609, in keys
    raise TypeError, "not indexable"

funny thing is, this used to work! what is wrong here? note, I am using webapp2.
enter image description here

also this page says: The FieldStorage instance can be indexed like a Python dictionary. It allows membership testing with the in operator, and also supports the standard dictionary method keys() and the built-in function len()

full stacktrace:

Traceback (most recent call last):
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3858, in _HandleRequest
    self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3792, in _Dispatch
    base_env_dict=env_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 580, in Dispatch
    base_env_dict=base_env_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2918, in Dispatch
    self._module_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2822, in ExecuteCGI
    reset_modules = exec_script(handler_path, cgi_path, hook)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2704, in ExecuteOrImportScript
    script_module.main()
  File "C:\myproject\main.py", line 16, in main
    run_wsgi_app(application)
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\util.py", line 98, in run_wsgi_app
    run_bare_wsgi_app(add_wsgi_middleware(application))
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\util.py", line 116, in run_bare_wsgi_app
    result = application(env, _start_response)
  File "C:\myproject\webapp2\__init__.py", line 1053, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\myproject\webapp2\__init__.py", line 1098, in wsgi_app
    self.handle_exception(request, response, e)
  File "C:\myproject\webapp2\__init__.py", line 1092, in wsgi_app
    self.router.dispatch(self, request, response, match)
  File "C:\myproject\webapp2\__init__.py", line 949, in dispatch
    handler.handle_exception(e, app.debug)
  File "C:\myproject\webapp2\__init__.py", line 942, in dispatch
    getattr(handler, method)(*args)
  File "C:\myproject\py\decorators.py", line 15, in decorated
    return _login_required (self) or func(self, *args, **kwargs)
  File "C:\myproject\py\document.py", line 42, in post
    if file and file.type and file.value:
  File "C:\Python25\lib\cgi.py", line 633, in __len__
    return len(self.keys())
  File "C:\Python25\lib\cgi.py", line 609, in keys
    raise TypeError, "not indexable"
TypeError: not indexable

the way i fixed it is to rip out if file: and instead check if self.request.POST has key 'file' using python has_key method

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心病无药医 2024-11-11 15:48:36

我在您的代码片段或堆栈跟踪中没有看到足够的数据来判断错误来自何处。相反,这里有一个完整的工作上传示例:

http ://blog.notdot.net/2009/9/Handling-file-uploads-in-App-Engine

I don't see enough data in your code snippet or your stack trace to tell where the error is coming from. In lieu of that, here's a full working upload example:

http://blog.notdot.net/2009/9/Handling-file-uploads-in-App-Engine

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文