如何在由流域创建图像的页面API中获取图像URL或下载图像的URL? [英] How to get image url or download url of images in pages API where image is created by a streamfield?
问题描述
在我的w应用程序中,我有一个流域,用于使用ImageChooserBlock以及标题和文本来上载图像。这意味着在单个流域中,我具有标题,文本和图像上传输入。我正在尝试在其余框架的页面API( localhost:8000 / api / v2 / pages / [page-id]
)中获取图片网址。但是此页面api仅给出了上传图像的图像ID,如下所示:
In my wagtail application I have a streamfield that is used to upload an image using ImageChooserBlock along with a title and text. That means in the single streamfield I have a title, a text and an image upload inputs. I'm trying to get the image url in the rest framework's pages API (localhost:8000/api/v2/pages/[page-id]
). But this pages api only gives the image id of the uploaded images as follows
{
"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 /
) m如下获得 download_url
If I access the images api(http://localhost:8000/api/v2/images/1/
) I'm getting the download_url
as follows
{
"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
或图像URL( localhost:8000 / api / v2 / pages / [page-id ]
)
My question is how I can get the download_url
or the image url in the pages API (localhost:8000/api/v2/pages/[page-id]
)
我的复仇者区块的streamfields blocks.py如下
My streamfields blocks.py for the avengers block is as follows
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中使用如下
This stream field is used in a content types model.py as follows
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"
推荐答案
将其添加到AvengersBlock中,并在
Add this to your AvengersBlock and when you call your API at
/ api / v2 / pages / ?type = home.AvengersPage& fields = content
/api/v2/pages/?type=home.AvengersPage&fields=content
您应该看到所需的JSON。
you should see the JSON you're looking for.
def get_api_representation(self, value, context=None):
""" Recursively call get_api_representation on children and return as a plain dict """
dict_list = []
for item in value["Avengers"]:
temp_dict = {
'title': item.get("title"),
'text': item.get("text"),
'image_url': item.get("image").file.url
# any other relevant fields of your model...
}
dict_list.append(temp_dict)
return dict_list
这篇关于如何在由流域创建图像的页面API中获取图像URL或下载图像的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!