Sam*_*uel 4 django wagtail wagtail-streamfield
我一直使用Wagtail作为无头CMS与前端应用程序一起使用,但是我注意到有关图像的一些限制.通常在您的jinja模板中,您将生成所需的正确图像大小,一切都很好,但是我无法在前端代码中访问这些帮助程序.我一直在尝试几件事.例如,要解决这个问题对于简单的页面模型及其字段,我可以像这样呈现自定义api字段:
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')),
...
]
Run Code Online (Sandbox Code Playgroud)
但是这不适用于streamfield,因为这些只会返回图像ID.所以我想我会使用Wagtail图像API,但是这也不允许我访问直接URL.
我发现一些google小组的答案参考了这个文档:http://docs.wagtail.io/en/v1.9/advanced_topics/images/image_serve_view.html
但是,此页面似乎不存在于最新版本的文档中,似乎不允许我从前端的URL生成图像.
有没有办法可以创建一个允许我根据其ID获取图像的网址?
例如: somehost:8000/images/1?width=200&height=200
或许我可以忽略其他一些解决方案.
我喜欢wangtail,但没有轻松访问图像网址真的限制其API使用,我希望有一个很好的解决方案.
谢谢
编辑:我设法在文档中找到了这个:http: //docs.wagtail.io/en/v1.11.1/advanced_topics/images/image_serve_view.html
然而他们说:
该视图在URL中采用图像ID,过滤规范和安全签名.如果这些参数有效,则它将提供与该条件匹配的图像文件.
但他们没有给出一个明确的例子,说明这样的请求是什么样的,或者我将如何生成该安全签名.
酮(略哈克)的方式来得到的图像再现作为StreamField数据结构的一部分是重写ImageChooserBlock的get_api_representation方法:
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,
}
Run Code Online (Sandbox Code Playgroud)
ImageChooserBlock在StreamField定义中使用此版本将为您提供"大"和"缩略图"再现,作为API响应的一部分,而不仅仅是图像ID.
| 归档时间: |
|
| 查看次数: |
727 次 |
| 最近记录: |