Tag for simple embed video from youtube watch link. It can be modified to manage size of embedded video.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | from django import template
import re
register = template.Library()
class YoutubeNode(template.Node):
"""Convenient tag, allowing only having a normal watch link, from yotube,
embed video in html."""
def __init__(self, parsed_link):
self.parsed_link = parsed_link
def render(self, context):
del_it = re.compile('(&.*)')
replace_it = re.compile('watch\?v=')
link = self.parsed_link.render(context)
link = del_it.sub('', link)
link = replace_it.sub('embed/', link)
video = """<iframe width="540" height="432" src="%s"
frameborder="0" allowfullscreen></iframe>""" % link
return video
@register.tag
def youtube(parser, token):
parsed_link = parser.parse(('endyoutube',))
# first token it's closing tag. delete_first_token
# just delete it - del self.tokens[0];)
print type(parsed_link)
parser.delete_first_token()
return YoutubeNode(parsed_link)
|
More like this
- Browser-native date input field by kytta 1 month, 1 week ago
- Generate and render HTML Table by LLyaudet 1 month, 2 weeks ago
- My firs Snippets by GutemaG 1 month, 3 weeks ago
- FileField having auto upload_to path by junaidmgithub 2 months, 4 weeks ago
- LazyPrimaryKeyRelatedField by LLyaudet 3 months, 1 week ago
Comments
Please login first before commenting.