Skip to content

Instantly share code, notes, and snippets.

@chhantyal
Last active April 10, 2019 21:07
Show Gist options
  • Save chhantyal/5396911 to your computer and use it in GitHub Desktop.
Save chhantyal/5396911 to your computer and use it in GitHub Desktop.
A normal Youtube url, saved in database is not enough to embed videos on app. This django template tag takes url, and returns embed url.
import urlparse
from django import template
register = template.Library()
def video_embed(context, url):
url_data = urlparse.urlparse(url)
query = urlparse.parse_qs(url_data.query)
try:
video_id = query["v"][0]
context['embed_url'] = ('http://youtube.com/embed/%s' % video_id)
except KeyError:
context['video_url'] = url
return context
@patrickb323
Copy link

Can you give an example of the template code that would use this templatetag? I am new to django and not sure how you are using "context". thanks!

@sarc007
Copy link

sarc007 commented Apr 10, 2019

Where do you put this file ? i.e. in the app level or project level ?
Secondly how do you install it ?
Just put the file in the app directory and it would start working ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment