March 2020
Intermediate to advanced
608 pages
17h 17m
English
In the utility_tags.py file, add the following content:
# myproject/apps/core/templatetags/utility_tags.pyimport refrom django import templatefrom django.utils.safestring import mark_saferegister = template.Library()""" FILTERS """MEDIA_CLOSED_TAGS = "|".join([ "figure", "object", "video", "audio", "iframe"])MEDIA_SINGLE_TAGS = "|".join(["img", "embed"])MEDIA_TAGS_REGEX = re.compile( r"<(?P<tag>" + MEDIA_CLOSED_TAGS + ")[\S\s]+?</(?P=tag)>|" + r"<(" + MEDIA_SINGLE_TAGS + ")[^>]+>", re.MULTILINE)@register.filterdef first_media(content): """ Returns the chunk of media-related markup from the html content """ tag_match = MEDIA_TAGS_REGEX.search(content) media_tag = "" if tag_match: media_tag = tag_match.group() return mark_safe(media_tag) ...