Wagtail从JSON API或直接获取/生成图像URL [英] Wagtail getting/generating image urls from JSON api or directly

查看:91
本文介绍了Wagtail从JSON API或直接获取/生成图像URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Wagtail作为无头CMS与前端应用程序一起使用,但是我注意到有关图像的一些限制.通常,在您的jinja模板中,您将生成所需的正确图像大小,并且一切都很好,但是我无法在前端代码中访问这些帮助程序.我一直在尝试几件事.例如,要解决简单页面模型及其字段的问题,我可以渲染一个自定义api字段,如下所示:

I've been using Wagtail as a headless CMS to use with a frontend application, However I noticed some limitations regarding images. Normally in your jinja template you would generate the correct image size you need and all is fine, However I don't have access to these helpers in my frontend code. I have been trying several things. for example, to solve this for simple page models and their fields I could render a custom api field like so:

api_fields = [
    # Adds information about the source image (eg, title) into the API
    APIField('feed_image'),

    # Adds a URL to a rendered thumbnail of the image to the API
    APIField('feed_image_thumbnail', serializer=ImageRenditionField('fill-100x100', source='feed_image')),
    ...
]

但是,这不适用于流场,因为它们只会返回图像ID.因此,我认为我会使用Wagtail图片API,但这也不允许我访问直接URL.

However this will not work for streamfield since these will only return the image id. So I figured I would use the Wagtail image API, however this does not allow me to access the direct URL either.

我找到了一些参考该文档的Google小组答案:http://docs.wagtail.io/en/v1.9/advanced_topics/images/image_serve_view.html

I find some google group answers refering to this documentation: http://docs.wagtail.io/en/v1.9/advanced_topics/images/image_serve_view.html

但是此页面似乎在最新版本的文档中不存在,并且似乎不允许我从前端的url生成图像.

However this page does not seem to exists in the latest version of the documentation and doesn't seem to allow me to generate images from a url on the frontend.

是否有一种方法可以创建一个允许我根据其ID提取图像的url?

Is there a way to perhaps create a url that allows me to fetch an image based on its ID?

例如: somehost:8000/images/1?width = 200& height = 200

或者我可能忽略了其他一些解决方案.

Or perhaps there is some other solution I am overlooking.

我喜欢吵架,但无法轻松访问图像URL确实限制了其API的使用,我希望对此有一个好的解决方案.

I love wagtail but not having easy access to image urls really limits its API usage, I hope there is a good solution for this.

谢谢

我设法在文档中找到了这一点: http://docs.wagtail.io/en/v1.11.1/advanced_topics/images/image_serve_view.html

I managed to find this in the docs: http://docs.wagtail.io/en/v1.11.1/advanced_topics/images/image_serve_view.html

但是他们说:

该视图在URL中获取图像ID,过滤器规格和安全签名.如果这些参数有效,它将提供一个符合该条件的图像文件.

The view takes an image id, filter spec and security signature in the URL. If these parameters are valid, it serves an image file matching that criteria.

但是他们没有给出一个清晰的示例,说明类似的请求是什么样的,或者我将如何生成该安全签名.

But they don't give a clear example of what a request like that would look like or how I would generate that security signature.

推荐答案

获取图像表示形式(作为StreamField数据结构的一部分)的一种方法(略为hacky)是重写 ImageChooserBlock get_api_representation 方法:

One (slightly hacky) way to get an image rendition as part of the StreamField data structure is to override ImageChooserBlock's get_api_representation method:

from wagtail.wagtailimages.blocks import ImageChooserBlock as DefaultImageChooserBlock

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,
            }

然后在StreamField定义中使用此版本的 ImageChooserBlock ,这将为您提供大"和缩略图"表示,作为API响应的一部分,而不仅仅是图像ID.

Using this version of ImageChooserBlock in your StreamField definition will then give you 'large' and 'thumbnail' renditions as part of the API response, rather than just the image ID.

这篇关于Wagtail从JSON API或直接获取/生成图像URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆