I’ve used this example to to implement some sort of guestbook, only I’m using django framework.
I can’t use db.key() function in the html file so I thought maybe I should pass the html with the string representation of the key and by this solve this issue. I checked the docs and it says
A key can be converted to a string by passing the Key object to str().
so I’ve used :
all_messages = db.GqlQuery( "SELECT * FROM Messages WHERE ANCESTOR IS :1 ORDER BY date DESC LIMIT 20" , image_key('name'))
for msg in all_messages:
msg.img_key_func = str(msg.key())
is this the correct way to convert key to string?
my html looks like this:
<ol name="list">
{% for message in all_messages %}
<li><div><img src="img?img_id={{ message.img_key_func }}"></img>{{ message.content }}</div></li>
{% endfor %}
</ol>
What i need to do is simply something like this example:
self.response.out.write("<div><img src='img?img_id=%s'></img>" %
greeting.key())
the outcome of the logs is: /img?img_id=None and thus, I cant display the images.
is there an easier way to do this?
Thanx!
EDIT 1: more info
the submit button triggers
class Sender(webapp.RequestHandler):
with the following:
def post(self):
accountName = self.request.get( "accountName" )
text = self.request.get( "text" )
message = Messages(parent=image_key('name'))
message.content = self.request.get( "text" )
avatar = self.request.get('img')
if avatar:
avatar = images.resize(self.request.get("img"), 32, 32)
image_full = self.request.get("img")
message.avatar = db.Blob(avatar)
message.imageblob = db.Blob(image_full)
message.put()
the Messages(db.Model) looks like:
class Messages(db.Model):
imageblob = db.BlobProperty()
avatar = db.BlobProperty()
content = db.StringProperty(multiline=True)
date = db.DateTimeProperty(auto_now_add=True)
def image_key(some_id='name'):
return db.Key.from_path('StartPage', 'name')
the question is,Should I store the avatar right after download Or I should resize very time a get_main_page is requested? this way I can avoid storing 2 images -one full size and another avatar.
should i still use image_key?
This:
is not true. You can use any functions or methods that don’t take arguments in a template. So, as thebjorn pointed out in the comment, this would work fine:
because the template language would call the
key()method and then convert it to string for output.