Getting images by ID or getting image url returned with page via api

1,511 views
Skip to first unread message

Chris Barry

unread,
Mar 2, 2015, 3:49:37 PM3/2/15
to wag...@googlegroups.com
Hi,

I've managed to return a feed image with my page results, but it's just sending me back the image ID's

Do I have to build my own api hook to handle getting images / image urls by id, or is there something built in?

Either an endpoint to load an image by an id, like /images/21

Or

make the page result return the URL of the image to load, rather than the ID.

Regards,

Chris

Matthew Westcott

unread,
Mar 2, 2015, 5:41:23 PM3/2/15
to wag...@googlegroups.com
Hi Chris,
Image objects in Wagtail don't natively have URLs of their own, because we can't necessarily rely on editors to upload them at a web-friendly resolution - it's the job of the template to request a 'rendition' object at the required size. Usually this would be done through a template tag such as {% image self.feed_image fill-320x200 %}, but you can obtain the rendition within Python code by calling rendition = image.get_rendition('fill-320x200') - or if you do in fact want the non-resized version, image.get_rendition('original'). The URL is then available as rendition.url.

Alternatively, you could leave it up to the consumer of the API to request the image at the required size, using Wagtail's 'external image generator' feature: http://docs.wagtail.io/en/stable/core_components/images/using_images_outside_wagtail.html
(Besides providing a user interface for generating sized images, it also provides a URL scheme for requesting them programmatically - although having said that, it appears that we haven't actually documented that scheme yet...)

- Matt

Chris Barry

unread,
Mar 4, 2015, 7:04:47 AM3/4/15
to wag...@googlegroups.com
Hi Matt,

Great feedback thanks :)

Any further info on that un-documented URL schedule would be really interesting (presumably you can restrict requested image sizes), as I've built something similar before, and just wanted to check before re inventing anything.

CB

Joss Ingram

unread,
Mar 4, 2015, 11:18:32 AM3/4/15
to wag...@googlegroups.com

Hi Chris,

We have a JSON feed from our site that returns images URLs that have a certain tag for another system to use. The wagtail admin creates a 165 width thumbnail which is what we use. As the JSON feed is just for thumbnails, if you need a bigger image I guess this won't work as there might not be a rendition entry for the image. We use a raw query something like below, you could easily change it to select by just an image ID.

SELECT max(wagtailimages_rendition.file) As thumbnail,wagtailimages_image.id,wagtailimages_image.title 
FROM wagtailimages_rendition 
INNER JOIN wagtailimages_image ON wagtailimages_rendition.image_id = wagtailimages_image.id 
WHERE wagtailimages_rendition.file LIKE '%%max-165x165%%'
GROUP BY wagtailimages_image.id,wagtailimages_image.title 

For a full URL take your base URL with a /media/ then the wagtailimages_rendition.file value.

H.T.H

Joss

Nidhi Sharma

unread,
Oct 11, 2017, 11:01:21 AM10/11/17
to Wagtail support
Not working if image is in ImageChooserPanel.
My image field is in model and that model is inherited to other model page. So error coming -- argument of type 'APIField' is not iterable

Matthew Westcott

unread,
Oct 11, 2017, 11:13:37 AM10/11/17
to wag...@googlegroups.com
Hi Nidhi,

We'd need more information to tell what's going wrong there - can you share your code?

Bear in mind that this is a very old thread - the current support for images in the API is documented at http://docs.wagtail.io/en/v1.12.2/advanced_topics/api/v2/configuration.html#images-in-the-api

Cheers,
- Matt
Reply all
Reply to author
Forward
0 new messages