Reputation: 148
I have the below output:
{'type': 'lead-image', 'value': {'id': 123, 'alt': 'a fish', 'format': 'full-width'}}
I want to display that specific image on my html template, how do i go about it?
Upvotes: 1
Views: 450
Reputation: 25317
The expand_db_attributes
method on wagtail.wagtailimages.rich_text.ImageEmbedHandler
does the translation you're looking for. Packaged up as a template tag (please adjust as appropriate for your own use-case - it looks like you've got a Python dict there, rather than a JSON string):
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)
Upvotes: 1