WutWut
WutWut

Reputation: 1234

Displaying youtube video in django 1.8 by passing id as CharField

I've been trying to display youtube videos by passing the video id in a class and replacing the value in a template.But it doesn't seem to be happening. My models.py

from django.db import models

class Rockinfo(models.Model):
   rock_name = models.CharField(max_length=200,default="ac/dc")
   rock_img = models.ImageField(upload_to="%Y/%m/%d",default="1992/08/92")
   rank = models.IntegerField(default=0)


def __str__(self):              
    return self.rock_name


class Rockvids(models.Model):
   rockername = models.ForeignKey(Rockinfo)
   vid_id = models.CharField(max_length=200,default="Z7JgY9zezj4")
   vid_name = models.CharField(max_length=300,default="Something Inside Me")
   vid_singer_name = models.CharField(max_length=300,default="Jonathan Reyes Myers")

def __str__(self):
    return self.vid_id

my admin.py

from django.contrib import admin
from .models import Rockinfo, Rockvids

class ChoiceInline(admin.TabularInline):
  model = Rockvids
  extra = 10

class RockinfoAdmin(admin.ModelAdmin):
  fieldsets = [
  ('The Fucking Band',               {'fields': ['rock_name']}),
  ('Image', {'fields': ['rock_img']}),
]
inlines = [ChoiceInline]  
list_display = ('rock_name', 'rock_img')
list_filter = ['rank']
search_fields = ['rock_name']


admin.site.register(Rockinfo, RockinfoAdmin)

my application's urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
   url(r'^$', views.IndexView.as_view(), name='index'),
   url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
]

My application's views.py file

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.views import generic
from .models import Rockvids, Rockinfo


class IndexView(generic.ListView):
   template_name = 'rockinglist/index.html'
   context_object_name = 'latest_rockinfo_list'

   def get_queryset(self):
    return Rockinfo.objects.order_by('-rank')[:50]


   class DetailView(generic.DetailView):
    model = Rockinfo
    template_name = 'rockinglist/detail.html'

index.html file

 {% if latest_rockinfo_list %}
 <ul>
 {% for rockinfo in latest_rockinfo_list %}
 <h1>{{ rockinfo.rock_name }}</a></li>
 <img src="img\{{ rockinfo.rock_img }}" alt="ac/dc">
 {% endfor %}
 </ul>
 {% else %}
 <p>No</p>
 {% endif %}

detail.html file

<h1>{{ rockinfo.rock_name }}</h1>
{% for choice in rockinfo.rockvids_set.all %}
<img id="hide" src="http://img.youtube.com/vi/{{ rockvids.vid_id   }}/hqdefault.jpg" data-video="https://www.youtube.com/embed/{{ rockvids.vid_id }}?autoplay=1" width="480" height="300"/>


{% endfor %}

I have been trying to display all the youtube videos once the user clicks on the image by using the videos id passed in Rockvidsclass but there seems to be some error in the loop. Thanks in advance.

Upvotes: 0

Views: 596

Answers (1)

Dan Russell
Dan Russell

Reputation: 970

In your template loop, you've defined the object as choice. Just refer to it that way rather than using rockvids. See below.

{% for choice in rockinfo.rockvids_set.all %}
<img id="hide" src="http://img.youtube.com/vi/{{ choice.vid_id   }}/hqdefault.jpg" data-video="https://www.youtube.com/embed/{{ choice.vid_id }}?autoplay=1" width="480" height="300"/>
{% endfor %}

Since you'll have multiple images, you probably want to change id="hide" to class="hide" as well, since there should only be one element with a given id on a page.

Upvotes: 1

Related Questions