Wagtail CMS通过API请求发送图像URL

时间:2019-10-08 18:56:12

标签: python django django-rest-framework wagtail

我在前端应用程序中使用w无头作为CMS。到目前为止,一切正常,但是调用API时我无法将图像导入前端应用程序。

这是我现在正在使用的模型,我也在使用流场:

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'),
    ]

我在这里获得了ImageChooserBlock的替代代码:Wagtail getting/generating image urls from JSON api or directly

这似乎不再起作用,我仍然从API得到如下响应:

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

我需要URL,以便可以在应用程序中显示它们。

还有其他想法吗?我在其他任何地方都找不到关于此的任何信息。

1 个答案:

答案 0 :(得分:0)

我在这里发布了类似问题的解决方案。

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

您的一般方法看起来正确。 您在使用

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

为您的API调用?

相关问题