[Django]-Trying to display a list of youtube videos into django template with django-embed-video

4👍

Thanks for the quick answer raratiru! It brought me on the idea to look into the code of the django-embed-video app itself. I fiddled around with it a bit and now it works just how I wanted it to work. The relevant code is:

videos.html

{% load embed_video_tags %}
{% for v in videos %}
    {% video v.video as my %}
        <iframe width="{{ 480 }}" height="{{ 320 }}" src="{{ my.url }}" 
        frameborder="0" allowfullscreen></iframe>
    {% endvideo %}
{% endfor %}
👤JRose

1👍

You need to acquire the EmbedVideoField() which is video according to models.py. Therefore, the loop should read something like:

{% extends "base.html" %}
{% load embed_video_tags %}
{% video item.video 'small' %}

{% block content %}

{% if videos %}

    {% for v in videos %}
    {{ v.video }}
    {% endfor %}

    {% else %}
    <p>No videos yet</p>

{% endif %}
{% endblock %}

It is possible that the EmbedVideoField() has more attributes that have to be accessed, you have to check the relevant docs. For example, if the embed code is stored in EmbedVideoField().embed_code you can reach it as such:

{% for v in videos %}
{{ v.video.embed_code }}
{% endfor %}

Leave a comment