views:

61

answers:

1

Hi All,

I am working on a Google app engine project where I have saved some images in the datastore. Now I have to resize these images and render them to the template. I have successfully fetched the images but I am not getting,how to render them with other data dictionaries to the template. I am using App engine patch with GAE.

Following is a code snippet that I am using in my view:

def getData(request,key):
forum = Topic.get(key)
picData = forum.creator.portfolio_set
response = ''
if picData:
    picture = picData[0].userpic
    response = HttpResponse(picture, mimetype="image/jpeg")
    #response['Content-Type'] = 'image/jpeg'
    response['Content-Disposition'] = 'inline'
return render_to_response(request,"forum.html",{"forum":forum,"pic":response })

Now, I am able to render the data within forum variable but not getting how to render the image associated with it. What should I modify in my views file and what should I use in my template to display the image rendered by the views file.

Please suggest.

Thanks in advance.

+1  A: 

Solution 1:

Add a generic view to get images e.g. /images/image-id should return image for that ID

def get_image(request, image_id): ....

picture = get from id
return HttpResponse(picture, mimetype="image/jpeg")

Now use image URLs inside your forum.html template, like you would use any normal image URL.

Solution 2: You can directly embed image using data:image see http://en.wikipedia.org/wiki/Data_URI_scheme

so try this

import base64

def getData(request,key):
    forum = Topic.get(key)
    picData = forum.creator.portfolio_set
    pictureSrc = ''
    if picData:
        picture = picData[0].userpic
        pictureSrc = "data:image;base64,%s"%base64.b64encode(picture)

    return render_to_response(request,"forum.html",{"forum":forum,"pic":pictureSrc })
Anurag Uniyal
We can not pass other data with HttpResponse with a template name. This will just display the image in a browser window. Correct me if I am wrong.
anand
anand: you use the HTML template to render an HTML page. Within that template, you have an IMG tag pointing to a different URL in your application, which returns the actual image. Your HTML shouldn't contain the actual images, but tags telling the browser how to get them.
Wooble
@wooble thanks for explaining, @anand, suppose if you had real image URLs, how would you have inserted them in template?, do the same thing here.
Anurag Uniyal
Thanks Anurag and Wooble, I know what you people are trying to convey me. You are asking me to create a view function which would return the image as http response and then put the url pointing to the view function in the <img> tag. However, what I am trying is, to render the images and data using render_to_response() function and display in the template. I hope I made you understand the thing. Please let me know if there is something which can be done using my code structure, else I am left with the above solution.
anand
@Anurag, Please have a look at this link http://www.greywyvern.com/code/php/binary2base64 I wanted to do something similar. i.e. to get the binary data and then embedd that into the html. I tried it but could not do.
anand
@anand, hmmm that is new to me, i will see and come back to you.
Anurag Uniyal
Cool, that works at least on chrome, i was wasn't knowing about data:image thing, +1 for that, but why you are not able to make it work, use base64 module and send to template
Anurag Uniyal
I tried but may be missing something... its not getting the base64 encoded data instead a blank string. Will try again.
anand
see answer update.
Anurag Uniyal