开发者

getting TypeError, "not indexable" upon execution of if self.request.POST['file'] inside post method of webapp2.RequestHandler

开发者 https://www.devze.com 2023-03-01 18:48 出处:网络
I am trying to upload a file using this code: <form id=\"form1\" action=\"convert\" enctype=\"multipart/form-data\" method=\"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.

getting TypeError, "not indexable" upon execution of if self.request.POST['file'] inside post method of webapp2.RequestHandler

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, respons开发者_开发知识库e, 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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号