Can someone verify/correct my method of accessing an image that is part of a ndb.Model class?
This is my MainPage handler:
class MainPage(webapp2.RequestHandler):
def get(self):
guestbook_name = self.request.get('guestbook_name',
DEFAULT_GUESTBOOK_NAME)
greetings_query = Greeting.query(
ancestor=guestbook_key(guestbook_name)).order(-Greeting.date)
greetings = greetings_query.fetch(10)
template_values = {
'greetings': greetings,
'guestbook_name': urllib.quote_plus(guestbook_name),
}
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render(template_values))
This is my index.html template code:
{% for greeting in greetings %}
<div><img src="/img?key_id={{ greeting.key.id() }}" />
</div>
{% endfor %}
This is my image handler:
class Image(webapp2.RequestHandler):
def get(self):
greeting = ndb.get(self.request.get('key_id'))
if greeting.avatar:
self.response.headers['Content-Type'] = 'image/png'
self.response.out.write(greeting.avatar)
self.response.write('it works')
else:
self.error(404)
self.response.out.write('No image')
The idea is that I store my image in a blob in my Greeting NDB Model. I then try to get that image by populating the main page with my MainPage handler which calls my Image handler. I pass the specific ndb entry key via my template and then retrieve the key from the template in the Image handler. I then attempt to display the blob attribute by getting the ndb entry using the key. Here's the error trace:
Aucun commentaire:
Enregistrer un commentaire