在模板中显示blob图像Google App Engine(Python)

时间:2013-07-30 16:39:00

标签: python google-app-engine blob blobstore

我正在尝试显示上传的blob图像。它似乎存储正常,但在调用时不会显示在html模板上。甚至似乎出现在伐木中。在查看其他示例时,我看到的唯一真正的区别是您必须迭代列表,但实际上只想拉一张图片。非常感谢您给予的任何建议。 -Thanks

处理程序:

class PictureHandler(BaseHandler2):
def get(self, **kwargs):
    user_session = self.user
    user_session_object = self.auth.store.get_session(self.request)

    user_info = models.User.get_by_id(long( self.user_id ))
    user_info_object = self.auth.store.user_model.get_by_auth_token(
        user_session['user_id'], user_session['token']) 

    user = self.session.get('user') 

    uploads = db.GqlQuery("SELECT * FROM UserPictureUpload WHERE user =:1 ORDER BY created DESC", user_info.username).get()

    upload_url = blobstore.create_upload_url('/upload')

    params = {
    'upload_url': upload_url,
    'user': user_info.username,
    'uploads': uploads      
    }       

    return self.render_template('picture.html', **params)


class UploadHandler(blobstore_handlers.BlobstoreUploadHandler, BaseHandler2):
def post(self):
    user_session = self.user
    user_session_object = self.auth.store.get_session(self.request)

    user_info = models.User.get_by_id(long( self.user_id ))
    user_info_object = self.auth.store.user_model.get_by_auth_token(
        user_session['user_id'], user_session['token']) 

    user = self.session.get('user') 

    title = self.request.get('title')
    pic = self.request.get("picfile")   


    if pic:
        picture = db.Blob(pic)

    upload = UserPictureUpload(title = title, picture=picture, user=user_info.username)         
    upload.put()
    self.redirect('/settings/picture')  


class ViewHandler(blobstore_handlers.BlobstoreDownloadHandler, BaseHandler2):
def get(self):    
    user_session = self.user
    user_session_object = self.auth.store.get_session(self.request)

    user_info = models.User.get_by_id(long( self.user_id ))
    user_info_object = self.auth.store.user_model.get_by_auth_token(
        user_session['user_id'], user_session['token']) 

    user = self.session.get('user') 


    upload_key_str = self.request.params.get('key')

    if upload_key_str:              

        upload = db.get(upload_key_str)
        if upload:
            logging.info('** Upload Found** --  %s' % upload.picture)  

        self.send_blob(upload.picture)      


    if not upload_key_str:

        self.error(404)
        return 

路由

RedirectRoute('/settings/picture', handlers.PictureHandler, name='picture', strict_slash=True), 
RedirectRoute('/upload', handlers.UploadHandler, name='upload', strict_slash=True), 
RedirectRoute('/view', handlers.ViewHandler, name='view', strict_slash=True)

Html:

<form action="{{ upload_url }}"  method="post" enctype="multipart/form-data">
<label for="title">Title:</label>
<input type="text" id="title" name="title" /><br />
<label for="upload">File:</label>
<input type="file" id="upload" name="picfile" /><br />
<input type="submit" value="Upload Picture" />
</form>
<br />
<img src='/view?key={{uploads.key()}}' alt='no image'/></img>

App Engine日志:

INFO 2013-07-30 16:11:20,946 handlers.py:875] **上传发现** - 内容类型:image / jpeg

内容长度:775702

Content-MD5:NWE0NGM3YmE1YmJlNGVjODY3MjMzZDY3ZTQ4MDY4NDg =

content-disposition:form-data; NAME = “picfile”;文件名= “Jellyfish.jpg”

X-AppEngine-Upload-Creation:2013-07-29 15:41:00.481000

INFO 2013-07-30 11:11:21,005 server.py:584]默认:“GET / view?key = ag5kZXZ-c2FuZGVuZ2luZXIeCxIRVXNlclBpY3R1cmVVcGxvYWQYgICAgJjxtgsM HTTP / 1.1”200 -

1 个答案:

答案 0 :(得分:0)

看起来你正在blobstore中上传blob但是从你的帖子上传处理程序创建一个键并将其作为另一个blob存储在picture属性上,而不是从上传的blobinfo中获取blobkey:https://developers.google.com/appengine/docs/python/tools/webapp/blobstorehandlers < / p>

你可以使用

upload = self.get_uploads()[0]
picture=upload.key()

那就是你在send_blob上的内容。