MultiValueDictKeyError使用Appengine SDK和Django进行文件上传

时间:2022-10-30 23:11:42

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.

我正在尝试使用appengine和django实现文件上传例程,我遇到了MultiValueDictKeyError错误。看来文件没有从网页到服务器。

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

这部分是学习练习,所以我不想使用djangoform为我处理数据。

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

我正在使用,SDK版本1.1.8,django版本1.1.0 alpha和google-appengine-django r68

My html looks like this:

我的HTML看起来像这样:

<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:

我的python看起来像这样:

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 /

/的MultiValueDictKeyError

"Key 'imgfile' not found in "

“密钥'imgfile'未找到”

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

请求方法:POST请求URL:http:// localhost:8000 / Exception类型:MultiValueDictKeyError异常值:“键'imgfile'未在”Exception Location:/Users/david/Sites/testsite/myapp/views.py in index中找到,第19行Python可执行文件:/ Library/Frameworks/Python.framework/Versions/2.5/Resources/Python.app/Contents/MacOS/Python Python版本: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)}>

/Users/david/Sites/testsite/myapp/views.py in index blFileData = request.FILES ['imgfile']。read()...▼Local vars变量值newtxt TestModel(** {'txt':u' World','img':无,'标题':u'Hello'})请求,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,与Gecko一样)Version / 3.2.1 Safari / 525.27.1','PATH_INFO':你'/','PATH_TRANSLATED':你'/用户/david/Sites/testsite/main.py','QUERY_STRING':'','REMOTE_ADDR':'127.0.0.1','REQUEST_METHOD':'POST' ,'SCRIPT_NAME':你','SERVER_NAME':'localhost','SERVER_PORT':'8000','SERVER_PROTOCOL':'HTTP / 1.0','SERVER_SOFTWARE':'开发/ 1.0','TZ':' UTC','USER_EMAIL':'','wsgi.errors':',模式'w'在0x130b0>,'wsgi.input':,'wsgi.multiprocess':False,'wsgi.multithread':False,' wsgi.run_once':是的,'wsgi.url_scheme':'http','wsgi.version':( 1,0)}>

Thoughts? Thanks, David

思考?谢谢,大卫

3 个解决方案

#1


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

由于某种原因,文件没有上传,或者如果有的话,它被绑定到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.

尝试记录request.FILES的值或尝试从处理程序检查其值,并查看request.FILES dict中实际包含的内容。这可能会带来一些突破。

To set a trace, you can use pdb.

要设置跟踪,可以使用pdb。

import pdb
pdb.set_trace()

#2


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.

我也遇到了这个错误,但那是因为我使用的是encoding =“multipart / form-data”,我认为这是错误的。我改为使用enctype =“multipart / form-data”并且它有效。

#3


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.

好的,最奇怪的事情发生了。我在昨天签字之前写了这个问题。当我今晚启动再次尝试时,在我对其进行任何更改之前,事情已经奏效。无论如何,谢谢你的帮助。

#1


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

由于某种原因,文件没有上传,或者如果有的话,它被绑定到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.

尝试记录request.FILES的值或尝试从处理程序检查其值,并查看request.FILES dict中实际包含的内容。这可能会带来一些突破。

To set a trace, you can use pdb.

要设置跟踪,可以使用pdb。

import pdb
pdb.set_trace()

#2


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.

我也遇到了这个错误,但那是因为我使用的是encoding =“multipart / form-data”,我认为这是错误的。我改为使用enctype =“multipart / form-data”并且它有效。

#3


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.

好的,最奇怪的事情发生了。我在昨天签字之前写了这个问题。当我今晚启动再次尝试时,在我对其进行任何更改之前,事情已经奏效。无论如何,谢谢你的帮助。

相关文章