需要使用Google App Engine Blobstore处理上传表单的帮助

需要使用Google App Engine Blobstore处理上传表单的帮助

问题描述:

我正在尝试学习blobstore API ......并且我能够成功上传文件并将它们还原回来,但是我没有任何运气试图将上传表单与常规网络表单结合起来以便能够到文件相关的额外信息,如文件的昵称。

I'm trying to learn the blobstore API... and I'm able to successfully upload files and get them back, but I'm not having any luck trying to combine an upload form with a regular webform to be able to associated extra info with the file, such as a nickname for the file.

以下是我一直在玩的简单应用程序的代码。它基于google提供的示例。

Below is the code for a simple app I've been playing with. It's based on the sample google provides.

#!/usr/bin/env python
#

import os
import urllib

from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class StoredFiles(db.Model):
    nickname = db.StringProperty()
    blobkey = blobstore.BlobReferenceProperty()

    @staticmethod
    def get_all():
        query = db.Query(StoredFiles)
        files = query.get()

        return files


def doRender(handler, page, templatevalues=None):
    path = os.path.join(os.path.dirname(__file__), page)
    handler.response.out.write(template.render(path, templatevalues))

class MainHandler(webapp.RequestHandler):
    def get(self):

        allfiles = StoredFiles.get_all()

        upload_url = blobstore.create_upload_url('/upload')

        templatevalues = {
                'allfiles': allfiles,
                'upload_url': upload_url,

            }
        doRender(self, 'index.html', templatevalues)

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files = self.get_uploads('file')
        blob_info = upload_files[0]

        self.redirect('/save/%s' % blob_info.key())

class SaveHandler(webapp.RequestHandler):

    def get(self, resource):

        newFile = StoredFiles()
        newFile.nickname = self.request.get('nickname')
        resource = str(urllib.unquote(resource))
        newFile.blobkey = resource

        newFile.put()

        self.redirect('/')

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self, resource):
        resource = str(urllib.unquote(resource))
        blob_info = blobstore.BlobInfo.get(resource)
        self.send_blob(blob_info)

def main():
    application = webapp.WSGIApplication(
          [('/', MainHandler),
           ('/upload', UploadHandler),
           ('/save/([^/]+)?', SaveHandler),
          ], debug=True)
    run_wsgi_app(application)

if __name__ == '__main__':
  main()

根据文档,blobstore处理程序应该通过blob键并将表单的其余部分传递给处理程序,将其重定向到... blob键是

According to the docs, the blobstore handler is supposed to pass through the blob key and the rest of the form to the handler its redirected to... blob key is coming through just fine, but nothing else is.

有人可以指出我搞乱了什么,或者指向我描述这个用例的好教程吗?

Can someone please point out where I'm messing up or point me to a good tutorial describing this use case?

问题在于,当您将请求重定向到/ save /%s时,您发布的表单数据会丢失,这是正常的。

The problem is that your posted form data is lost when you redirect the request to "/save/%s", which is normal.

而不是重定向,你应该把你的代码放到UploadHandler里面,就像这样(未经测试的代码):

Instead of redirecting, you should put your code inside UploadHandler, like this (untested code) :

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        try:
            upload_files = self.get_uploads('file')
            blob_info = upload_files[0]

            newFile = StoredFiles()
            newFile.nickname = self.request.get('nickname')
            newFile.blobkey = blob_info.key()
            newFile.put()

            self.redirect('/')
        except:
            self.redirect('/upload_failure.html')

请参阅此文档中的相似示例文档: http://code.google.com/appengine/docs/python/tools/webapp/blobstorehandlers.html#BlobstoreUploadHandler

See this page from the docs for a similar example : http://code.google.com/appengine/docs/python/tools/webapp/blobstorehandlers.html#BlobstoreUploadHandler