0

我一直在用头撞墙,由于某种原因,我无法将 Google App Engine 的不同方面联系在一起以使其工作。

基本上我想让用户将照片上传到 Blobstore,我在下面的代码中工作,然后我想将 BlobKey 放入将存储在数据库实体中的列表中。所以这是我上传图像的代码,在这里我可以在哪里获取 BlobKey 以便我可以存储它?

class MainHandler(BlogHandler):
    def get(self):
        upload_url = blobstore.create_upload_url('/upload')
        self.response.out.write('<html><body>')
        self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
        self.response.out.write("""Upload File: <input type="file" name="file"><br> <input type="submit" name="submit" value="Submit"> </form></body></html>""")
        #there is a lot more code in here where I get all the following info but it isn't relevant
        location_db = Location(
                    description=description,
                    submitter=submitter,
                    user_id=user_id, 
                    title=title,
                    locationtype = locationtype)
            #This is what I would like to do but I don't know where to get thr BlobKey
            location_db.blobRefs.append(BlobKey)
            location_db.put()

        for b in blobstore.BlobInfo.all():
            self.response.out.write('<li><a href="/serve/%s' % str(b.key()) + '">' + str(b.filename) + '</a>')

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

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self, blob_key):
        blob_key = str(urllib.unquote(blob_key))
        if not blobstore.get(blob_key):
            self.error(404)
        else:
            self.send_blob(blobstore.BlobInfo.get(blob_key), save_as=True)
4

1 回答 1

3

这里:

blob_info = upload_files[0]
self.redirect('/serve/%s' % blob_info.key())

我想就是这样

blob_info.key()

你不见了。抓住它,把它塞进你的清单。文档还指出:

在此处理程序中,您可以将 blob 键与应用程序数据模型的其余部分一起存储。Blob 键本身仍可从数据存储中的 Blob 信息实体访问。 https://developers.google.com/appengine/docs/python/blobstore/overview#Serving_a_Blob

于 2012-09-22T23:41:23.887 回答