views:

1180

answers:

3

Can anyone help me spot my problem here:

I'm trying to implement a file upload routine using appengine and django, and I've run into a MultiValueDictKeyError error. It appears that the file is not making it from the webpage to the server.

Some part of this is a learning exercise, so I don't want to use a djangoform to process the data for me.

I'm using, SDK version 1.1.8, django version 1.1.0 alpha, and google-appengine-django r68

My html looks like this:

<form method="POST" action="." enctype="multipart/form-data">
  Title: <input type="text" name="title"/>
  Text: <input type="text" name="txt"/>
  Image: <input type="file" name="imgfile"/>

  <input type="submit"/>
</form>

My python looks like this:

def index(request):

if request.POST:
   newtxt = TestModel()
   newtxt.title = request.POST.get('title', '')
   newtxt.txt = request.POST.get('txt', '')
   blFileData = request.FILES['imgfile'].read()
   if blFileData:
       newtxt.img = blFileData
   newtxt.put()

return render_to_response('index.html', ({'filestore': query,}))

The error looks like this:

MultiValueDictKeyError at /

"Key 'imgfile' not found in "

Request Method: POST Request URL: http://localhost:8000/ Exception Type: MultiValueDictKeyError Exception Value: "Key 'imgfile' not found in " Exception Location: /Users/david/Sites/testsite/myapp/views.py in index, line 19 Python Executable: /Library/Frameworks/Python.framework/Versions/2.5/Resources/Python.app/Contents/MacOS/Python Python Version: 2.5.2

/Users/david/Sites/testsite/myapp/views.py in index blFileData = request.FILES['imgfile'].read() ... ▼ Local vars Variable Value newtxt
TestModel(**{'txt': u'World', 'img': None, 'title': u'Hello'}) request , POST:, COOKIES:{}, META:{'APPLICATION_ID': 'google-app-engine-django', 'AUTH_DOMAIN': 'gmail.com', 'CONTENT_LENGTH': '21', 'CONTENT_TYPE': 'application/x-www-form-urlencoded', 'CURRENT_VERSION_ID': '1.1', 'GATEWAY_INTERFACE': 'CGI/1.1', 'HTTP_ACCEPT': 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5', 'HTTP_ACCEPT_LANGUAGE': 'en', 'HTTP_CONNECTION': 'keep-alive', 'HTTP_HOST': 'localhost:8000', 'HTTP_REFERER': 'http://localhost:8000/', 'HTTP_USER_AGENT': 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1', 'PATH_INFO': u'/', 'PATH_TRANSLATED': u'/Users/david/Sites/testsite/main.py', 'QUERY_STRING': '', 'REMOTE_ADDR': '127.0.0.1', 'REQUEST_METHOD': 'POST', 'SCRIPT_NAME': u'', 'SERVER_NAME': 'localhost', 'SERVER_PORT': '8000', 'SERVER_PROTOCOL': 'HTTP/1.0', 'SERVER_SOFTWARE': 'Development/1.0', 'TZ': 'UTC', 'USER_EMAIL': '', 'wsgi.errors': ', mode 'w' at 0x130b0>, 'wsgi.input': , 'wsgi.multiprocess': False, 'wsgi.multithread': False, 'wsgi.run_once': True, 'wsgi.url_scheme': 'http', 'wsgi.version': (1, 0)}>

Thoughts? Thanks, David

+2  A: 

For some reason the file is not getting uploaded, or if at all, it's getting bound to a different key in request.FILES

Try logging the value of request.FILES or try inspecting its value from the handler and see what is really contained in the request.FILES dict. That might lead to some breakthrough.

To set a trace, you can use pdb.

import pdb
pdb.set_trace()
Baishampayan Ghose
A: 

Okay, the strangest thing happened. I wrote this question before I signed off yesterday. When I booted up tonight to try it again, the thing worked before I made any changes to it. Thanks anyway for the help.

A: 

I've faced this error also but that is because I used encoding="multipart/form-data" which, I think, is wrong. I changed to use enctype="multipart/form-data" and it works.