I'm working on a little side project that involves posting an avatar to a users profile page, seems straight forward enough. I'm following the instructions from the "Using the Images Python API" on the GAE web site.
The sample they provide doesn't seem to work with Django though. Searching around here, I found a thread with a similar issue, but said the resolution came from using a newer version of Django with GAE. I'm trying to avoid this work around, and get it running with the build in API. I am new to both Python and Django, but from what I can gather, the image isn't properly being posted to the function that transforms the image and sends it to the Datastore. Here is my code:
def post(self):
contacts = Contact()
if users.get_current_user():
contacts.owner = users.get_current_user()
else:
self.redirect('/')
contacts.fname = self.request.get('fname')
contacts.lname = self.request.get('lname')
contacts.pnum = self.request.get('pnum')
img = self.request.FILES['file'].read()
img.resize(32,32)
contacts.avatar = db.Blob(images.resize(img,32,32))
contacts.put()
self.redirect('/')
All the other fields are added correctly, except the avatar field. The modified request string comes from the previously stated thread that contained a fix. When attempting to access the avatar, and this gets kind of odd, the page displays a broken image, rather than the default image I setup. This leads me to believe that, although the field is null or missing, there is still something there to draw a unique key when requesting it. Here is the display image function:
class Image (webapp.RequestHandler):
def get(self):
contacts = db.get(self.request.get("img_id"))
if contacts.avatar:
image = contacts.avatar
self.response.headers['Content-Type'] = "image/png"
self.response.out.write(HttpResponseRedirect(contacts.avatar))
else:
self.response.out.wrute(HttpResponseRedirect("/static/image_not_found.gif"))
I've tried a number of different variations when trying to get this working as well, so please trust that I did, initially, use the block of code from the instructions.
Any help is greatly appreciated.
Thanks in advance.