Reputation: 3100
I have a simple blog website in django and I want to include video embeds using this script:
<script src= "http://player.twitch.tv/js/embed/v1.js"></script>
<div id="youtubeplayer"></div>
<script type="text/javascript">
var options = {
width: 800,
height: 500,
video: "627627612"
};
var player = new Twitch.Player("youtubeplayer", options);
player.setVolume(0.5);
</script>
I have a URL field on the Post model. I want to then take that URL and only take part of it to fill in the "video" parameter in the above script. So, for example we have Twitch video URL https://www.twitch.tv/videos/494181151
and I want to take the 494181151
and convert to string so I can fill it in to the "video" parameter above in an html template.
Is this possible and how can I go about doing it? I know I have to check for the URL and then check if it contains part of the URL like this:
{% if url %}
{% if 'twitch.tv/videos' in url %}
But I'm not sure how to get the last part of the URL.
Thanks!
EDIT:
Thanks to Nalin I was able to create a method in the Post model to extract the video ID correctly. However, when I use the variable in the html template, all I see is some Javascript printing onto the web page. var options = { width: 800, height: 500, ...
Here is my updated html template:
{% if post.video_URL %}
<script src= "http://player.twitch.tv/js/embed/v1.js"></script>
<div id='youtubeplayer'></div>
<script type="text/javascript">
var options = {
width: 800,
height: 500,
video: "{{ post.get_video_id }}"
};
var player = new Twitch.Player("youtubeplayer", options);
player.setVolume(0.5);
</script>
{% endif %}
Here's the model method if needed but it seems to be working correctly if I simply print {{ post.get_video_id }}
. There's only an issue when I try to include it in the above javascript to embed the Twitch video.
class Post(models.Model):
user = models.ForeignKey(User, on_delete= models.CASCADE)
title = models.CharField(max_length=100, blank=False)
content_text = models.TextField(blank=False, max_length=3000)
created_date = models.DateTimeField(auto_now_add=True)
score = models.IntegerField(blank=True, null=True)
slug = AutoSlugField(populate_from='title',
unique_with=['user__username', 'created_date'])
tags = TaggableManager()
video_URL = models.URLField(blank=True, verbose_name='Video/VOD/Clip URL')
def __str__(self):
return self.title
def get_video_id(self):
video_id = self.video_URL.split('/')[-1] # will return str
return video_id
Thanks!!
Upvotes: 0
Views: 473
Reputation: 2342
You can simply split the url to get the video id.
video_url = 'https://www.twitch.tv/videos/494181151'
video_id = video_url.split('/')[-1] # will return str
Then you can return video_id
in your context. Then in your template, you can do something like:
<script type="text/javascript">
{% if video_id %}
var options = {
width: 800,
height: 500,
video: "{{ video_id }}"
};
var player = new Twitch.Player("youtubeplayer", options);
player.setVolume(0.5);
{% endif %}
</script>
You might have to add extra checks while splitting the string.
Upvotes: 1