Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-24-2010, 09:51 AM   PM User | #1
smickle
New to the CF scene

 
Join Date: Jul 2010
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
smickle is an unknown quantity at this point
Upload images: How to obtain image key? for reference in HTML

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..

Any ideas??

Code:
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'
        else:
            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(greeting.avatar)
        else:
            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()
        greeting.put()
        self.redirect('/')
          

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

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

Code:
<!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">
<head>
    <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" />
</head>
<body>
<div id="header">
  <h1 align="center">Problem, Reaction, Solution</h1></div>


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

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

</form>

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


<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>

</body>
</html>
THANKS!!!
smickle is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:54 PM.


Advertisement
Log in to turn off these ads.