1

我有以下输出:

{'type': 'lead-image', 'value': {'id': 123, 'alt': 'a fish', 'format': 'full-width'}}

我想在我的 html 模板上显示该特定图像,我该怎么做?

4

1 回答 1

1

上的expand_db_attributes方法wagtail.wagtailimages.rich_text.ImageEmbedHandler可以完成您正在寻找的翻译。打包为模板标签(请根据您自己的用例进行适当调整 - 看起来您在那里有一个 Python 字典,而不是 JSON 字符串):

import json
from django.utils.html import escape
from wagtail.wagtailimages.rich_text import ImageEmbedHandler


register = template.Library()

@register.simple_tag
def json_to_image(json_string):
    data = json.loads(json_string)

    # expand_db_attributes expects to receive HTML-escaped attributes,
    # so explicitly escape the alt attribute here (assuming it's not already
    # escaped in your json)
    data['value']['alt'] = escape(data['value']['alt'])

    return ImageEmbedHandler.expand_db_attributes(data['value'], False)
于 2015-08-19T18:48:08.727 回答