获取错误:无法找到* blob_key *的会话

时间:2013-03-17 12:27:15

标签: python google-app-engine blobstore

我试图制作一个用户可以上传文件的表单。我有一个表单,它接受用户参数并将它们发布到UploadHandler,后者映射到作为参数传递给renderfunction的url。它可以提供表单,但在提交文件时,它会将我重定向到空白页面,控制台会向我显示错误:

 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-form('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>

采用args形式的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 个答案:

答案 0 :(得分:1)

可能是这个会话已过期或已被使用。使用create_upload_url创建的网址有一个到期时间(我想10分钟)。在实际发布之前,您可能希望使用JavaScript刷新该URL。

此外,如果由于任何原因将文件上传到Blobstore中,您将无法再次使用相同的URL,它只适用于一个请求(可能有多个文件)。

在您的示例中,尝试删除所有特定于文件的检查,尝试上传内容并检查数据存储查看器中的管理控制台(如果您有任何blob或会话密钥)。可以通过以下URL访问:

 http://localhost:8080/_ah/admin/datastore