Reputation: 21
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
Upvotes: 1
Views: 1158
Reputation: 21
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
.
Upvotes: 1
Reputation: 15509
Generator itself doesn't allow you to do that on the fly, it generates file-like object, which means it doesn't save the file on disk, which means no filename and so, no URL name..
Either you save the object and generate url manually or you can use ImageCacheFile
like this:
from imagekit.cachefiles import ImageCacheFile
file = ImageCacheFile(image_generator, name='my_image.thumb.jpg')
file.generate()
print file.url
Upvotes: 0