Getting a TypeError that is not indexable when executing if self.request.POST ['file'] inside the post method webapp2.RequestHandler

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 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, it worked! what's wrong here? Notice I'm using webapp2. enter image description here

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

full stack:

 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 

as I fixed this, I need to tear out if file: and instead check if self.request.POST has a key "file" using the python has_key method

+4
source share
1 answer

I don't see enough data in the code snippet or your stack trace to indicate where the error is coming from. Instead, here is a complete working boot example:

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

0
source

All Articles