使用Django将图像存储在App Engine上 [英] Storing Images on App Engine using Django

查看:114
本文介绍了使用Django将图像存储在App Engine上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to upload and save a resized image in a db.BlobProperty field on Google App Engine using Django.

我的视图的相关部分请求如下所示:

the relevant part of my view that process the request looks like this:

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

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

Which seems like it would be the logical django equivalent of the example in the docs:

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('/')

(来源:< a href =http://code.google.com/appengine/docs/python/images/usingimages.html#Transform =noreferrer> http://code.google.com/appengine/docs/python/图像/ usingimages.html#转换)

但是,我不断收到一条错误:NotImageError / Empty图像数据。

But, I keep getting an error that says: NotImageError / Empty image data.

并引用此行:

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

我无法访问图像数据。似乎没有被上传,但我无法弄清楚为什么。我的表单有enctype =multipart / form-data等等。我认为我是指图像数据有问题。 request.POST.get('image')但是我不知道如何引用它。任何想法?

I'm having trouble getting to the image data. Seems like it's not being uploaded but I can't figure out why. My form has the enctype="multipart/form-data" and all that. I think something's wrong with how I'm referring to the image data. "request.POST.get('image')" but I can't figure out how else to reference it. Any ideas?

提前感谢

推荐答案

经过一些指导从hcalves我想出了这个问题。首先,与App Engine捆绑的默认版本的Django版本为0.96,而且框架如何处理上传的文件。不过为了保持与旧版应用的兼容性,您必须明确告诉App Engine使用Django 1.1,如下所示:

After some guidance from "hcalves" I figured out the problem. First of all, the default version of Django that comes bundled with App Engine is version 0.96 and how the framework handles uploaded files has changed since then. However in order to maintain compatibility with older apps you have to explicitly tell App Engine to use Django 1.1 like this:

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

你可以详细了解应用引擎文档中的详细信息。

You can read more about that in the app engine docs.

好的,这里是解决方案:

Ok, so here's the solution:

from google.appengine.api import images

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

然后,从数据存储区再次提供动态映像,构建图像处理程序像这样:

Then, to serve the dynamic images back again from the datastore, build a handler for images like this:

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

这篇关于使用Django将图像存储在App Engine上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆