Good Afternoon,
I'm currently trying to build something incredibly simple inside of the Google AppEngine. The goal is to build a simple photo sharing application that will connect back to my iPhone application. It's all a learning experience for both Python and Objective-C.
(I've been a PHP programmer for quite some time).
The goal, create URL's that look like the following: /img/{{ model.key.id }}
The problem is that it seems no matter how I do the python script, I either end up with an error or simply get nothing to display on my template page that's wrapped in the FOR statement.
My App.yaml File:
application: randomwebappname
version: 1
runtime: python
api_version: 1
handlers:
- url: /media
static_dir: media
- url: /b/.*
script: beta.py
login: required
- url: /.*
script: main.py
My Model (inside beta.py):
class Photo(db.Model):
author = db.StringProperty()
title = db.StringProperty()
slugline = db.StringProperty()
content = db.StringProperty(multiline=True)
coordinates = db.StringProperty()
avatar = db.BlobProperty()
date = db.DateTimeProperty(auto_now_add=True)
My Class For Viewing The Image Page:
class ViewPage(webapp.RequestHandler):
def get(self, id):
template_values = {
'image': image,
}
path = os.path.join(os.path.dirname(__file__), 'templates/view.html')
self.response.out.write(template.render(path, template_values))
I tried all of the following in my class, but they all end in failure. I tried them with key, key.name, and key.id in the URL:
photos = db.Query(Photo).filter('key', slug).fetch(limit=1)
photos = Photo.get_by_key_name(id)
photos = Photo.get_by_key_name(key)
key = db.Key.from_path('Photo', id)
photos = db.GqlQuery("SELECT * FROM Photo WHERE __key__ = :key", key=key)
photos = db.get(photo_key)
photos = self.request.get("id")
My URL's:
application = webapp.WSGIApplication([
('/b/', HomePage),
('/b/upload', UploadPage),
('/b/add', MainPage),
('/b/img', Image),
('/b/img/([-\w]+)', ViewPage),
('/b/submit', Submission)
], debug=True)
The Template Query:
{% for photo in photos %}
<img alt="" title="" src="img?img_id={{ photo.key }}" alt="main image" />
{% endfor %}
This seems like it would be something incredibly simple and I know I'm missing something, but I'm just not sure where it's at. I would write this in PHP, but I like the concept of AppEngine and I've said above, it's a good Python learning experience.
As a side note, this application does work on the homepage of the site. I simply have a GQL query and it outputs the images fine, it just fails when I try to go to the /img/id pages.
Any advice guys (and gals)? Thanks in advance!
UPDATE #1:
As per Jonathan's request, the following is the Image class:
class Image (webapp.RequestHandler):
def get(self):
photo = db.get(self.request.get("img_id"))
if photo.avatar:
self.response.headers['Content-Type'] = "image/png"
self.response.out.write(photo.avatar)
else:
self.response.out.write("No image")
Also, after posting this, I realized that this was part of the problem as I was trying to do /img as the actual image and /img/ to display the view page. I've since changed this and now have a working model. But it's based upon the Key and not key.id:
URL:
('/b/i', ViewPage)
New ViewPage Class:
class ViewPage(webapp.RequestHandler):
def get(self):
image = db.get(self.request.get("id"))
template_values = {
'image': image,
}
path = os.path.join(os.path.dirname(__file__), 'templates/view.html')
self.response.out.write(template.render(path, template_values))
So... to get to the View page (which includes comments and such), you now have to go to the following URL: /b/i?img_id={{ image.key }}
Atleast the page is working now, but I would much prefer to get the page to look like the following as stated above: /b/img/{{ image.key.id }}
Update #2: ViewPage class updated as well as URL's:
class ViewPageV2(webapp.RequestHandler):
def get(self, id):
images = [ db.get(id) ]
template_values = {
'image': image,
}
path = os.path.join(os.path.dirname(__file__), 'templates/view.html')
self.response.out.write(template.render(path, template_values))
New URL: ('/b/image/([-\w]+)', ViewPageV2),
The following are two screenshots with one being proof of id "1" existing and the error that is coming with it currently.
Again, Thanks All!