0

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

enter image description here

Thanks!!

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

thank you! How can I do this if I'm looping through multiple Post objects? My view is using ` posts = Post.objects.all`, then I loop through them in the html template. Can I set video_id in the html template?
@winston create a instance def get_video_id(self) method in your models.py and return video_id from that method and call that method in template as {{ post.get_video_id }}.
thank you very much, that method correctly extracts the video ID from the URL. However, it looks like the javascript code is printing onto the website via the html template. I updated my question. I feel I am finally very close to solving this thanks to you!
it is kind of unexpected, can you add some photo of that behavior? also add you model method in your post.
I solved the issue! It was due to a conflicting javascript plugin. Thank you for helping me solve the original problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.