5
votes

I am unable to get the image to display on the page. I can store it just fine.

These are the handlers:

class disp_image(webapp.RequestHandler):
    def get(self):
        key = self.request.get('key')
        image = Images.get(key)
        if image:
            self.response.headers['Content-Type'] = "image/png"
            return self.response.out.write(images.image)
        else:
            self.response.headers['Content-Type'] = "image/png"
            return self.response.out.write("/static/unknown.gif")

class Profile(MainHandler):
    def get(self):      
        if self.user:
            self.render('profile.html', username = self.user.name, email = self.user.email, first_name = self.user.first_name, last_name = self.user.last_name, country = self.user.country, prov_state = self.user.prov_state, city_town = self.user.city_town)
        else:
            self.redirect('/register')

class Change_Profile_Image(MainHandler):
    def get(self):
        if self.user:
            self.render('change_profile_image.html', username = self.user.name, firstname=self.user.first_name)
        else:
            self.render('change_profile_image.html')

    def post(self):
        images = Images()
        imageupl = self.request.get('img')
        images.image = db.Blob(imageupl)
        images.put()
        self.redirect('/profile')

db stuff is a follows:

class Images(db.Model):
    image = db.BlobProperty()

class User(db.Model):
    name = db.StringProperty(required=True)
    pw_hash = db.StringProperty(required=True)
    email = db.StringProperty(required=True)
    first_name = db.StringProperty()
    last_name = db.StringProperty()
    country = db.StringProperty()
    prov_state = db.StringProperty()
    city_town= db.StringProperty()
    clan= db.StringProperty()
    handle= db.StringProperty()

The form:

<form enctype="multipart/form-data" method="post">
       <input type="file" name="img" />
</form>

My template substitution bit of code:

<img src="/disp?key={{image}}" /></img>
3
What problem are you having? (404, garbage content, etc?) Also can you post your app.yaml and route settings. - Sebastian Kreft
The {{image}} is not being passed. Also, i've subbed in the image key manually, and still no image is being displayed. - suitegamer
Yaml {{ application: suitegamer version: 1 runtime: python27 api_version: 1 threadsafe: true handlers: - url: /static static_dir: static - url: /img static_dir: img - url: /.* script: main.app libraries: - name: jinja2 version: latest - name: PIL version: "1.1.7"}} - suitegamer
I would try first to see if /disp?key=<some_key> is working. Can you post the routes associated with each handler? - Sebastian Kreft
You've shown us (almost) all the relevant code, which is great, but you haven't actually explained what the problem is. - Nick Johnson

3 Answers

2
votes

For one, I think you're mixing up the image key and the image binary data.

The Template

You should be writing the image key (string) to your template:

 <img src="/disp?key={{image_key}}" />

i.e. your final html should look like the following if you just pass image.key() to your template:

 <img src="/disp?key=AC3CK3333KCCK2K213" />

You can think of it as a two step process. You render your template. The user browser renders the html and does a get to your image handler. The image handler then sends the binary data.

(Btw, I was under the impression you close your html tags with either <img ... /> or <img ...></img>, not both <img .../></img>)

Image Handler

In your image handler, disp_image, you should be returning the binary data of the image. It looks like you first condition is okay, but I think your else condition will just return the text "/static/unknown.gif" and not the actual binary data of the gif file.

    else:
        self.response.headers['Content-Type'] = "image/png"
        return self.response.out.write("/static/unknown.gif")

You should either store unknown.gif in your datastore and refer to it by key or you should be writing the "/static/unknown.gif" url to your template in lieu of "/disp?key=..."

Lastly, the simplest way to test whether your image handler is working is when you plug in <domain>/disp?key=... into your browser URL bar, it should load the desired image.

2
votes

You do not have to serve images yourself. Google can do the job with Google High-Performance Image Serving for free! See: https://developers.google.com/appengine/docs/python/images/functions get_serving_url(blob_key, size=None, crop=False, secure_url=None) Where you only have to get the serving url once and save it for use in your HTML templates in your Images model class.

1
votes

I am not sure, but I think you should not return from the get method at the disp_image class.

class disp_image(webapp.RequestHandler):
    def get(self):
        key = self.request.get('key')
        image = Images.get(key)
        if image:
            self.response.headers['Content-Type'] = "image/png"
            self.response.out.write(images.image)
        else:
            self.response.headers['Content-Type'] = "image/png"
            self.response.out.write("/static/unknown.gif")

Could you explain where is the template rendering called from? Where are you passing the image attribute?