在我的 wagtail 应用程序中,我有一个流字段,用于使用 ImageChooserBlock 上传图像以及标题和文本。这意味着在单个流字段中我有一个标题、一个文本和一个图像上传输入。我正在尝试在其余框架的页面 API 中获取图像 url (localhost:8000/api/v2/pages/[page-id]
)。但是这个pages api只给出了上传图片的image id,如下
{
"type": "avengers",
"value": {
"title": "Tony Stark",
"avengers": [
{
"image": 1, /******* this is the image id returned ********/
"title": "Iron Man",
"text": "Iron man is now in framework"
}
]
},
"id": "2f27cb24"
}
如果我访问图像 api(http://localhost:8000/api/v2/images/1/
)我得到了download_url
如下
{
"id": 1,
"meta": {
"type": "wagtailimages.Image",
"detail_url": "http://localhost/api/v2/images/1/",
"tags": [],
"download_url": "/media/original_images/avenger.jpeg"
},
"title": "avenger.jpeg",
"width": 400,
"height": 400
}
我的问题是我怎样才能得到download_url
或页面 API 中的图像 url (localhost:8000/api/v2/pages/[page-id]
)
我的复仇者块的streamfieldsblocks.py如下
class AvengersBlock(blocks.StructBlock):
title = blocks.CharBlock(required=True, help_text="Add your title")
Avengers = blocks.ListBlock(
blocks.StructBlock(
[
("image", ImageChooserBlock(required=True)),
("title", blocks.CharBlock(required=True, max_length=40)),
("text", blocks.TextBlock(required=True, max_length=200))
]
)
)
class Meta: # noqa
template = "streams/Avengers_block.html"
icon = "placeholder"
label = "Avengers"
该流字段在内容类型 model.py 中使用,如下所示
from django.db import models
from wagtail.admin.edit_handlers import FieldPanel, StreamFieldPanel
from wagtail.core.fields import StreamField
from wagtail.core.models import Page
from wagtail.api import APIField
from apps.common.streams import blocks
class AvengersPage(Page):
tempalte = "avengers/avengers_page.html"
content = StreamField(
[
("avengers", blocks.AvengersBlock())
],
null=True,
blank=True,
)
subtitle = models.CharField(max_length=100, null=True, blank=True)
content_panels = Page.content_panels + [
FieldPanel("subtitle"),
StreamFieldPanel("content"),
]
api_fields = [
APIField("subtitle"),
APIField("content")
]
class Meta: # noqa
verbose_name = "Avengers Page"