[Answer]-How to output just html image tag when I have text and html in django template output?

1👍

Since you are getting that as just text, your best solution would be to write a template filter that would strip content not in the <img> html tag.

If the object were a ImageField (or FileField), you can call on the url attribute only, {{ movie.img.url }}

update

Ok, here’s a basic, probably too naive template filter for your use.

from django import template
from django.template.defaultfilters import stringfilter
import re

register = template.Library()

@register.filter(is_safe=True)
@stringfilter
def get_img_tag(value):
    result = re.search("<.*?>", value)
    if result:
        return result.group()
    return value

Use:

{{ movie.img|get_img_tag|safe }}
👤j_syk

Leave a comment