1

你好,我是新来的鹡鸰。最初,我看到我们可以通过 Draftail 方便地嵌入视频,但是我无法给它任何样式。因此我交换了方法,到目前为止,我一直在使用以下实现将视频嵌入到我的项目中。

块.py

class VideoBlock(blocks.StreamBlock):
'''rich text'''
title = blocks.CharBlock(required=True, help_text="Add your Title")
texts = blocks.TextBlock(required=True, help_text="Add your additional text")
embed = EmbedBlock()

class Meta:
    template = "streams/video_block.html"
    icon = "edit"
    label = "Full Rich Text

video_block.html

{% load wagtailembeds_tags %}


{%for content in self %}
    <div class="container" style="text-align: center;">
    {{content}}
    {% embed page.video_url %}
    </div>
{%endfor%}

话虽如此,我仍然不知道如何正确调整它的大小。我希望我的网站看起来像这样:这里

4

1 回答 1

2

我在她身上找到了一个非常好的链接:这里

如果有人感兴趣,这是我的代码:

video_block.html

{% load wagtailembeds_tags %}
{% load video_tag%}
<br>
{%for content in self %}
    <div class="container">
        {%for con in content.value%}
        <h1>{{con.title}}</h1>
        <p>{{con.text}}</p>
        <div class="embed-responsive embed-responsive-16by9">
            <iframe class="embed-responsive-item" src="{{ con.video.url |embedurl}}?rel=0" allowfullscreen></iframe>
          </div>
        {%endfor%}
    </div>
{%endfor%}

模板标签/video_tag.py

import re
from django import template

register = template.Library()

@register.filter(name="embedurl")
def get_embed_url_with_parameters(url):
    print('we inside this emb')
    if "youtube.com" in url or "youtu.be" in url:
        regex = r"(?:https:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)"  # Get video id from URL
        embed_url = re.sub(
            regex, r"https://www.youtube.com/embed/\1", url
        )  # Append video id to desired URL
        print(embed_url)
        embed_url_with_parameters = embed_url + "?rel=0"  # Add additional parameters
        return embed_url_with_parameters
    else:
        return None
于 2020-05-09T12:01:45.357 回答