Skip to content

How to get url of each image? #48

Answered by dzhuang
reinatch asked this question in Q&A
Discussion options

You must be logged in to vote

As documented in the doc, those ids are the pks of the target_model instances (which should be garlleryfield.BuiltInGalleryImage instances if you did not do model customization). You can query the instances through:

 from garlleryfield import BuiltInGalleryImage
 images = [ 0, 2, 3, 5, 6, 7, 7]
 img_urls = []
 for pk in images:
     img_urls.append(BuiltInGalleryImage.objects.get(pk=pk).url)
 print(img_urls)

Or more simpler, if the Gallery instance is my_gallery, it can be:

 img_urls = [img.url for img in my_gallery.images.objects.all()]

The latter is better as you don't need to know the actual model of the image instances.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by reinatch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants