【问题标题】:Wagtail CMS send image URL over API requestWagtail CMS 通过 API 请求发送图像 URL
【发布时间】:2019-10-08 18:56:12
【问题描述】:

我正在使用 wagtail headless 作为前端应用程序的 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,以便在我的应用程序中显示它们。

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

【问题讨论】:

    标签: python django django-rest-framework wagtail


    【解决方案1】:

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

    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 调用?

    【讨论】:

    • 是的,我确保请求 /api/v2/pages/?type=home.HomePage&fields=body。仍然不起作用。我已经在源代码中挖掘了几个小时,看起来这段代码应该可以覆盖 ImageChooserBlock,然后流场将序列化的内容写入数据库,然后由 api 调用重新使用。我尝试让 def get_api_representation() 返回一些简单的东西,例如: def get_api_representation(self, value, context=None): if True: return { 'title': 'test', }
    猜你喜欢
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 2016-07-19
    • 2021-08-02
    • 2019-11-24
    • 2020-05-12
    相关资源
    最近更新 更多