【问题标题】:Storing Images on App Engine using Django使用 Django 在 App Engine 上存储图像
【发布时间】:2009-10-24 03:51:06
【问题描述】:

我正在尝试使用 Django 在 Google App Engine 上的 db.BlobProperty 字段中上传并保存调整大小的图像。

我认为处理请求的相关部分如下所示:

image = images.resize(request.POST.get('image'), 100, 100)
recipe.large_image = db.Blob(image)
recipe.put()

这似乎是文档中示例的逻辑 django 等价物:

from google.appengine.api import images

class Guestbook(webapp.RequestHandler):
  def post(self):
    greeting = Greeting()
    if users.get_current_user():
      greeting.author = users.get_current_user()
    greeting.content = self.request.get("content")
    avatar = images.resize(self.request.get("img"), 32, 32)
    greeting.avatar = db.Blob(avatar)
    greeting.put()
    self.redirect('/')

(来源:http://code.google.com/appengine/docs/python/images/usingimages.html#Transform

但是,我不断收到一条错误消息:NotImageError / Empty image data。

并且指的是这一行:

image = images.resize(request.POST.get('image'), 100, 100)

我无法获取图像数据。好像没有上传,但我不知道为什么。我的表单有 enctype="multipart/form-data" 和所有这些。我认为我引用图像数据的方式有问题。 “request.POST.get('image')”,但我不知道如何引用它。有什么想法吗?

提前致谢。

【问题讨论】:

    标签: django google-app-engine


    【解决方案1】:

    在“hcalves”的一些指导下,我发现了问题所在。首先,与 App Engine 捆绑在一起的默认 Django 版本是 0.96 版,从那时起框架处理上传文件的方式发生了变化。但是,为了保持与旧应用程序的兼容性,您必须明确告诉 App Engine 使用 Django 1.1,如下所示:

    from google.appengine.dist import use_library
    use_library('django', '1.1')
    

    您可以阅读更多关于 in the app engine docs 的信息。

    好的,解决方法如下:

    from google.appengine.api import images
    
    image = request.FILES['large_image'].read()
    recipe.large_image = db.Blob(images.resize(image, 480))
    recipe.put()
    

    然后,要再次从数据存储中返回动态图像,请为图像构建一个处理程序,如下所示:

    from django.http import HttpResponse, HttpResponseRedirect
    
    def recipe_image(request,key_name):
        recipe = Recipe.get_by_key_name(key_name)
    
        if recipe.large_image:
            image = recipe.large_image
        else:
            return HttpResponseRedirect("/static/image_not_found.png")
    
        #build your response
        response = HttpResponse(image)
        # set the content type to png because that's what the Google images api 
        # stores modified images as by default
        response['Content-Type'] = 'image/png'
        # set some reasonable cache headers unless you want the image pulled on every request
        response['Cache-Control'] = 'max-age=7200'
        return response
    

    【讨论】:

      【解决方案2】:

      您通过 request.FILES['field_name'] 访问上传的数据。

      http://docs.djangoproject.com/en/dev/topics/http/file-uploads/


      阅读更多关于 Google 的 Image API 的信息,在我看来你应该做这样的事情:

      from google.appengine.api import images
      
      image = Image(request.FILES['image'].read())
      image = image.resize(100, 100)
      recipe.large_image = db.Blob(image)
      recipe.put()
      

      request.FILES['image'].read() 应该可以工作,因为它应该是 Django 的 UploadedFile 实例。

      【讨论】:

      • 感谢您的帮助 hcalves,我一定还是做错了什么,因为我更改如下: image = images.resize(request.FILES['image'], 300, 200) recipe.large_image = db.Blob(image) recipe.put() 现在我在 resize() 调用中收到 BadImageError。此外,GAE 默认使用 django .96 ......也许我应该尝试改变它?我也尝试过 request.FILES['image'].read() 但这没有用。它告诉我“'dict'对象没有属性'read'”我明天会继续搞砸这个。但如果你有更多的建议,我会全力以赴。再次感谢。
      猜你喜欢
      • 2010-11-09
      • 2010-12-12
      • 1970-01-01
      • 2011-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      相关资源
      最近更新 更多