Last active
April 10, 2019 21:07
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
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!