2015-08-19 43 views
1

我有下面的输出模板渲染图像?从一个JSON字符串[鹡鸰CMS]

+0

已经发布一个直接的答案,但我很好奇这种情况是怎么出现的,以及这些数据来自哪里......你是否在做StreamField转换的某种丰富文本? – gasman

+0

嗨gasman分享电子邮件是结束您的代码非常感谢您输入 – paul100

回答

1

上的expand_db_attributes方法会翻译您要找的内容。打包作为一个模板标签(请以适合自己使用情况调整 - 它看起来像你已经有了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) 
+0

如果我没有alt属性,我拥有的是id和标题? – paul100