views:

397

answers:

3

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.

+1  A: 

In your image serving code, you're writing out a redirect, with the 'URL' for the redirect being the image data - eg, you're redirecting users to "%PNG...". You need to write out the response data directly.

Besides that, what is HttpResponseRedirect? It's not part of the webapp framework.

Also, have you checked the datastore viewer to see if the image is being uploaded correctly? Try hashing the image before uploading it, and comparing it to the hash you see in the datastore (remove the resize call first). Is it the same?

Nick Johnson
A: 

The following line won't work in Django 0.96, which is the default version on GAE:

img = self.request.FILES['file'].read()

I was the one who posted a similar question before (I think this is the thread you're referring to: http://stackoverflow.com/questions/1616890/storing-images-on-app-engine-using-django/1688498#1688498)

In your question you mention that you don't want to use a newer version of Django because it's a "workaround". I think what you're missing is that they Google App Engine already comes with Django 1.0 and 1.1 installed, they just made it so you have to explicitly specify which version you want to run in order to avoid breaking the apps that were first deployed built on 0.96.

You don't have to include the whole django library with your deploy. You just have to change your bootstrap file to tell App Engine that you when you say:

import django

That you want it to hand you Django 1.1.

You can read more about that here: http://code.google.com/appengine/docs/python/tools/libraries.html

However, if you the solution described in that other thread, it will work: http://stackoverflow.com/questions/1616890/storing-images-on-app-engine-using-django/1688498#1688498

Hope that helps.

Henrik Joreteg
A: 

Here's my code:

        if 'image' in request.FILES:
            imageFile = request.FILES['image'] #in forms.py: image  = forms.FileField(required=False)
            fileSize = imageFile.size  
            fileType = imageFile.content_type 
            filename = imageFile.name


            cPhoto = CarPhoto(car=car)
            cPhoto.content = db.Blob(imageFile.read())    
            cPhoto.filename = User.objects.make_random_password(32) +'.jpg'                         
            cPhoto.put()

then I display the image like this:

def show_car_photo(request, filename):

cPhoto = CarPhoto.gql('Where filename=:filename', filename=filename).get()
if cPhoto is None:
    return HttpResponse(filename + ': Image Not Found')

response = HttpResponse(mimetype="image/jpeg")
response.content = cPhoto.content
return response
dannyroa