View Full Version : Upload images: How to obtain image key? for reference in HTML

10-24-2010, 10:51 AM
I cannot get this to work...

I want to somehow find the reference of the image i just uploaded and simply display in a div.

I am calling {greeting.theKey} in my HTML but it never returns anything...

I have tried all combos, cannot get it to work...

Im using google app engine for this as well..:thumbsup:

Any ideas??

import os
from google.appengine.ext.webapp import template

import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.api import images

class Greeting(db.Model):
author = db.UserProperty()
content = db.StringProperty(multiline=True)
date = db.DateTimeProperty(auto_now_add=True)
location = db.StringProperty(multiline=True)
theKey = db.StringProperty(multiline=True)

class MainPage(webapp.RequestHandler):
def get(self):
greetings_query = Greeting.all().order('-date')
greetings = greetings_query.fetch(10)

if users.get_current_user():
url = users.create_logout_url(self.request.uri)
url_linktext = 'Logout'
url = users.create_login_url(self.request.uri)
url_linktext = 'Login'

template_values = {
'greetings': greetings,
'url': url,
'url_linktext': url_linktext,

path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))

class Image (webapp.RequestHandler):
def get(self):
greeting = db.get(self.request.get("img_id"))
#theKey = greeting.key()

if greeting.avatar:
self.response.headers['Content-Type'] = "image/png"
self.response.out.write("No image")

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')
greeting.location = self.request.get('location')
avatar = images.resize(self.request.get('img'), 32, 32)
greeting.avatar = db.Blob(avatar)
greeting.theKey = self.request.get('key')
#greeting.theKey = Image.greeting.key()

application = webapp.WSGIApplication(
[('/', MainPage),
('/sign', Guestbook)],

def main():

if __name__ == "__main__":

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<title>Problem, Reaction, Solution</title>
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="js/myjs.js" type="text/javascript"></script>
<link href="styles/mystyles.css" rel="stylesheet" type="text/css" />
<div id="header">
<h1 align="center">Problem, Reaction, Solution</h1></div>

<form id="myForm" action="/sign" method="post">

<table class="style1">
<td class="style2">Screen Name</td>
<td><input id="firstName" type="text" /></td>
<td class="style2">Problem</td>
<td><input id="problem" type="text" /></td>
<td class="style2">Location</td>
<td><input id="location" name="location" type="text" /></td>
<td class="style2">Comments:</td>
<td><textarea id="content" cols="40" name="content" rows="5"></textarea></td>
<td class="style2">&nbsp;</td>
<td class="style2">&nbsp;</td>
<input id="guestbtn" type="submit" value="Report IT!"/>
<input id="Reset1" type="reset" value="reset" />


<form action="/sign" enctype="multipart/form-data" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="file" name="img"/></div>
<div><input type="submit" value="Sign Guestbook"></div>

<h3>Problems Reported</h3>

{% for greeting in greetings %}

<div id="myImage"><img src='img?img_id={{greeting.theKey}}'></div>

{% if greeting.author %}
<div class="pyDiv"><b>{{ greeting.author.nickname }}</b> wrote:

{% else %}
<div class="pyDiv">Some random unit wrote:

{% endif %}

<blockquote>{{greeting.location}} {{ greeting.content|escape }}</blockquote></div>

{% endfor %}

<a href="{{ url }}">{{ url_linktext }}</a></div>

<div id="commentsDiv"></div>