Need help processing your upload form using the Google App Engine Blobstore

I am trying to learn the blobstore API ... and I can successfully upload files and return them, but I am not able to combine the upload form with a regular web form in order to be able to connect additional information with the file, such as an alias for the file.

Below is the code for the simple application I was playing with. It is based on an example provided by Google.

#!/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()

According to the docs, the blobstore handler should go through the blob key, and the rest of the form should go through the handler redirected to ... the blob key goes fine, but nothing else.

Can someone point out where I am confused or point me to a good tutorial describing this use case?

+3
1

, "/save/% s", .

UploadHandler, ( ):

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

+4

Source: https://habr.com/ru/post/1768548/