我通過實現自定義模板標籤解決了這個問題。這可能是一個快速而有點骯髒的解決方案(自定義提供商可能是正式的方式),但它的工作,這對我來說已經足夠了。
用戶插入一個普通的URL。如果該網址適用於Youtube或Vimeo,則自定義模板標籤會照管它。否則,該模板將使用默認的W providers提供程序。
項目/ templatetags/custom_template_tags.py:
import re
from django import template
register = template.Library()
@register.filter
def get_embed_url_with_parameters(url):
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
embed_url_with_parameters = embed_url + '?rel=0' # Add additional parameters
return embed_url_with_parameters
elif 'vimeo.com' in url:
embed_url = url.replace('vimeo.com', 'player.vimeo.com/video')
embed_url_with_parameters = embed_url + '?loop=0&title=0&byline=0&portrait=0'
return embed_url_with_parameters
else:
return None
/project/templates/video_embed.htm:
{% load wagtailcore_tags %}
{% load wagtailembeds_tags %}
{% load custom_template_filters %}
{% with value.embed.url as regular_url %}
{% with regular_url|get_embed_url_with_parameters as embed_url %}
<div class="container">
<div class="block-description cvast-embed cvast-spacer-top">
<div align="center">
<h5>{{ value.title }}</h5>
{% if embed_url is None %}
{% embed regular_url %}
{% else %}
<iframe src="{{ embed_url }}" frameborder="0" allowfullscreen></iframe>
{% endif %}
</div>
</div>
</div>
{% endwith %}
{% endwith %}
項目/ models.py:
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.blocks import StructBlock
from wagtail.wagtailembeds.blocks import EmbedBlock
from wagtail.wagtailadmin.edit_handlers import FieldPanel
class EmbedVideoBlock(StructBlock):
embed = EmbedBlock()
class Meta:
template = "blocks/embed_video_block.htm"
class YourPage(Page):
video = EmbedVideoBlock()
content_panels = Page.content_panels + [
FieldPanel('video'),
]
來源
2017-10-20 18:19:25
VME
你有沒有試過'{%embed video.url | add:'&rel = 0'%}'? – Dekker
不幸的是,這並不奏效。 – KevTuck