2009-10-24 17 views
5

Djangoを使用して、Google App Engineのdb.BlobPropertyフィールドにサイズ変更された画像をアップロードして保存しようとしています。Djangoを使用してApp Engineに画像を保存する

要求は次のようになります処理する私の見解の関連部分:(

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

:それのように思える

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

は、ドキュメントの例の論理的なDjangoのと同等になりますソース:http://code.google.com/appengine/docs/python/images/usingimages.html#Transform

しかし、私は言う:NotImageError/Empty画像データ。

と、この行を参照:

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

私はトラブルの画像データに取得を抱えています。アップロードされていないようですが、理由を理解できません。私のフォームは、enctype = "multipart/form-data"とそのすべてを持っています。画像データをどのように参照しているか、何か問題があると思います。 "request.POST.get( 'image')"しかし、私はそれを参照する他の方法を把握することはできません。何か案は?

ありがとうございます。

答えて

9

"hcalves"からのガイダンスの後、私は問題を理解しました。まず、App EngineにバンドルされているDjangoのデフォルトバージョンはバージョン0.96で、アップロードされたファイルをフレームワークがどのように処理したかが変更されています。しかし、あなたが明示的にこのようにDjangoの1.1を使用するようにApp Engineのを伝える必要があり、古いアプリケーションとの互換性を維持するために:

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 
3

アップロードしたデータにはrequest.FILES ['field_name']でアクセスします。 Googleの画像APIの詳細を読む

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


、あなたはこのような何かをやるべき私には思える:。

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 [ 'イメージ'](読み取り)は、DjangoのUploadedFileインスタンスになっているため動作します。

+0

あなたの助けのhcalvesてくれてありがとう変更されたため、私は何か間違ったことをしているに違いありません。 image = images.resize(リクエスト。ファイル['image']、300,200) recipe.large_image = db.Blob(画像) レシピ。 put() 今、Resize()呼び出しでBadImageErrorが発生しています。また、GAEはデフォルトでdjango .96を使用しています...多分私はそれを変更しようとする必要がありますか? 私もrequest.FILES ['image']。read()を実行しようとしましたが、うまくいきませんでした。 「dict」オブジェクトには「読み込み」という属性がありません。「 私はこの明日をつぶやきます。しかし、もしあなたが何か提案を持っていれば、私はすべて耳にします。再度、感謝します。 –

関連する問題