获取错误:找不到*blob_key*的会话
我正在尝试制作一个表单,让用户可以上传文件。我有一个表单,可以接收用户的输入,并把这些输入发送到一个叫做UploadHandler的处理程序,这个处理程序的地址是通过render函数传递的参数来指定的。到目前为止,这个表单可以正常显示,但当我提交文件时,它却把我重定向到一个空白页面,控制台显示了一个错误:
ERROR 2013-03-17 11:53:30,769 dev_appserver_blobstore.py:404] Could not find session for ahhkZXZ-a3Vyc3NhbW1hbmZhdHRuaW5nYXJyGwsSFV9fQmxvYlVwbG9hZFNlc3Npb25fXxgxDA
INFO 2013-03-17 11:53:30,779 dev_appserver.py:3104] "POST /_ah/upload/ahhkZXZ- a3Vyc3NhbW1hbmZhdHRuaW5nYXJyGwsSFV9fQmxvYlVwbG9hZFNlc3Npb25fXxgxDA HTTP/1.1" 404 -
我还是新手,搞不清楚发生了什么。我明白这些映射可能出了问题,但我还是不明白为什么它没有把我重定向到正确的处理程序(SummaryHandler)?
这个类是用来提供文件上传表单的:
class CreateHandler(BaseHandler):
def get(self):
self.render('create.html', upload_url = blobstore.create_upload_url('/upload'))
这个html表单('create.html'):
<h2 class="main-title">Upload a file!</h2>
<form action="{{upload_url}}" method="post" enctype="multipart/form-data">
<label>
<div>Upload:</div>
<input type="file" name="file" accept="application/pdf"><br>
</label>
<div class="error">
{{file_error}}
</div>
<br>
<input type="submit" name="submit" value="Submit">
</form>
处理表单输入的uploadhandler:
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file')
blob_info = upload_files[0]
#validation of form-vars
have_error=False
file_data=dict()
if not (blob_info.content_type == 'application/pdf'):
file_data['file_error']="You can only upload files in pdf-format."
have_error=True
if have_error:
self.render('create.html', **file_data)
else:
#'parent' is just some stuff for organising the database.
#Not important in this context
the_file = A_File(parent = summary_key(),
file_key=str(blob_info.key()))
the_file.put()
if the_file:
self.redirect('/summary/%s' % str(the_file.key().id()))
else:
_error="Could not find the file"
self.render('welcome.html', error=_error)
应用程序的处理程序及其映射关系:
app = webapp2.WSGIApplication([
(r'/', WelcomeHandler),
(r'/create', CreateHandler),
(r'/upload', UploadHandler),
(r'/summary/(\d+)', SummaryHandler) #I have not included this handler in the text
], debug=True)
1 个回答
1
可能是因为这个会话过期了或者已经被使用了。通过 create_upload_url
创建的链接是有有效期的(我记得是10分钟)。在真正上传之前,你可能需要用JavaScript刷新一下这个链接。
另外,如果因为某种原因文件已经上传到Blobstore,你就不能再用同样的链接了,这个链接只能用一次(不过可以上传多个文件)。
在你的例子中,试着去掉所有与文件相关的检查,上传一些东西,然后通过管理控制台在Datastore Viewer中查看是否有任何的blob或者会话密钥。你可以通过这个链接访问:
http://localhost:8080/_ah/admin/datastore