pythondjangoimagekit

Django Imagekit - how to get the url not in the template?


I have been using django imagekit without any problems in the templates but now I need to get the url in the view not the template, following this example in the imagekit docs:

source_file = open('/path/to/myimage.jpg')
image_generator = Thumbnail(source=source_file)
result = image_generator.generate()

So I did this:

course_img = open(settings.MEDIA_ROOT+str(course.image), 'rb+')     
image_generator = myapp_images.Thumbnail100x100(source=course_img)
result = image_generator.generate()

And then, I try to get the url from the "result" variable, but I don't know how:

details_html += "<img class='img-rounded' src='"+ str(result) +"'/>

I have been trying with str(result), result.url, result.name, etc... with no luck, any idea how to get it? Thx


Solution

  • I got the solution from the imagekit author in the google imagekit group:

    In my case I wanted to interact with the Cache File, in order to do that we have to wrap my generator with an ImageCacheFile, like this:

    from imagekit.cachefiles import ImageCacheFile
    
    image_generator = myapp_images.Thumbnail00x100(source=course.image)
    result = ImageCacheFile(image_generator)
    

    so the result object it is what I was expecting, now I can read the url just using result.url.