from django.template.defaultfilters import stringfilter from django import template import re register = template.Library() @register.filter @stringfilter def youtube(url): regex = re.compile(r"^(http://)?(www\.)?(youtube\.com/watch\?v=)?(?P[A-Za-z0-9\-=_]{11})") match = regex.match(url) if not match: return "" video_id = match.group('id') return """ """ % (video_id, video_id) youtube.is_safe = True # Don't escape HTML