Django并使用解析来嵌入视频以获取YouTube视频ID

时间:2018-03-31 04:06:33

标签: python django django-templates

我觉得我非常接近于让它发挥作用。我厌倦了一切,但视频没有显示。问题出在我认为的HTML中。

型号:

  class Video(models.Model):
    link = models.URLField()

    def video_id(link):

        query = urlparse(link)
        if query.hostname == 'youtu.be':
            return query.path[1:]
        if query.hostname in ('www.youtube.com', 'youtube.com'):
            if query.path == '/watch':
                p = parse_qs(query.query)
                return p['v'][0]
            if query.path[:7] == '/embed/':
                return query.path.split('/')[2]
            if query.path[:3] == '/v/':
                return query.path.split('/')[2]




    def __unicode__(self):
        return self.link

的观点:

def index(request):
full_list = Video.objects.all()
return render_to_response('index.html', {'full_list': full_list})

HTML:

{%load staticfiles %}
{%load static%}
<h1>YouTube list</h1>
{% if full_list %}
  <ul>
     {% for video in full_list %}
       <li>


  <iframe width="560" height="345" src="{{Video.video_id}}?rel=0" frameborder="0" allowfullscreen></iframe>
      </li>

{% endfor %}
 </ul>
{% endif %}

我已经在这里待了三天了,我似乎无法让视频出现。

1 个答案:

答案 0 :(得分:0)

虽然您已在import tkinter as tk root = tk.Tk() # write the validation command def _phone_validation(text_size_if_change_allowed): if len(text_size_if_change_allowed) == 3: entry2.focus_set() if len(text_size_if_change_allowed)<4: return True return False # register a validation command valid_phone_number = root.register(_phone_validation) # reference the validation command entry = tk.Entry(validate='key', validatecommand=(valid_phone_number,'%P' )) entry2 = tk.Entry() entry.pack() entry2.pack() root.mainloop() 类中创建了一个函数,但您并未将“self”作为第一个参数传递。此外,您尝试将其作为属性而不是模板上的函数进行访问。

固定代码应该是这样的:

Video

然后在模板上,使用它与在属性上使用它相同:

#  Model:
class Video(models.Model):
    link = models.URLField()

    @property
    def video_id(self):
        query = urlparse(self.link)
        if query.hostname == 'youtu.be':
            return query.path[1:]
        if query.hostname in ('www.youtube.com', 'youtube.com'):
            if query.path == '/watch':
                p = parse_qs(query.query)
                return p['v'][0]
            if query.path[:7] == '/embed/':
                return query.path.split('/')[2]
            if query.path[:3] == '/v/':
                return query.path.split('/')[2]

    def __unicode__(self):
        return self.link

否则,你可以使它成为一个功能:

<iframe width="560" height="345" src="{{Video.video_id}}?rel=0" frameborder="0" allowfullscreen></iframe>

但是你需要把它称为一个函数:

#  Model:
class Video(models.Model):
    link = models.URLField()

    def video_id(self):
        query = urlparse(self.link)
        if query.hostname == 'youtu.be':
            return query.path[1:]
        if query.hostname in ('www.youtube.com', 'youtube.com'):
            if query.path == '/watch':
                p = parse_qs(query.query)
                return p['v'][0]
            if query.path[:7] == '/embed/':
                return query.path.split('/')[2]
            if query.path[:3] == '/v/':
                return query.path.split('/')[2]

    def __unicode__(self):
        return self.link