1
votes

I am using wagtail headless as a CMS for my front-end application. Everything is working fine so far, but I can not get my images into my front-end application when calling the API.

Here is the model I am working with right now, I am using streamfield too:

from django.db import models

from wagtail.core.models import Page
from wagtail.core.fields import StreamField
from wagtail.core import blocks
from wagtail.admin.edit_handlers import FieldPanel, StreamFieldPanel
from wagtail.images.blocks import ImageChooserBlock as DefaultImageChooserBlock
from wagtail.api import APIField

class ImageChooserBlock(DefaultImageChooserBlock):
    def get_api_representation(self, value, context=None):
        if value:
            return {
                'id': value.id,
                'title': value.title,
                'large': value.get_rendition('width-1000').attrs_dict,
                'thumbnail': value.get_rendition('fill-120x120').attrs_dict,
            }

class HomePage(Page):
    body = StreamField([
        ('heading', blocks.CharBlock(classname="full title")),
        ('paragraph', blocks.RichTextBlock()),
        ('image', ImageChooserBlock()),
    ],)

    content_panels = Page.content_panels + [
        StreamFieldPanel('body'),
    ]

    api_fields = [
        APIField('body'),
    ]

I got the override code for ImageChooserBlock here : Wagtail getting/generating image urls from JSON api or directly

This no longer seems to be working, I am still getting a response from API as follows:

"title": "now",
    "body": [
        {
            "type": "image",
            "value": 1,
            "id": "f5714257-bf5d-469b-9b4f-875212ec8d8d"
        }

I need the URL so I can display them in my application.

Any other ideas? I can't find any info about this anywhere else.

1

1 Answers

0
votes

I post a solution to a similar problem here.

How to get image url or download url of images in pages API where image is created by a streamfield?

Your general approach looks right. Are you using

/api/v2/pages/?type=home.HomePage&fields=body

For you API call?