我正在关注the documentation,也正在关注this example,以便在我的Wagtail网站中创建自定义OEmbed Finder。 (最终,我想将YouTube视频的HTML输出修改为使用youtube-nocookie.com
域,而不是youtube.com
。)
我已在myapp.embeds.finders.oembed.py
中创建了此文件:
from wagtail.embeds.finders.oembed import OEmbedFinder
class YouTubeOEmbedFinder(OEmbedFinder):
def find_embed(self, url, max_width=None):
embed = super().find_embed(url, max_width)
# Just to see that it's doing something:
embed['html'] = '<p>Hello</p>'
return embed
并在我的设置中添加了它:
from wagtail.embeds.oembed_providers import youtube
WAGTAILEMBEDS_FINDERS = [
{
'class': 'myapp.embeds.finders.oembed.YouTubeOEmbedFinder',
'providers': [youtube],
},
{
# Handles all other oEmbed providers the default way
'class': 'wagtail.embeds.finders.oembed',
},
]
但是没有什么不同-YouTube嵌入的标准在发布的页面中。据我所知,从未调用过我的find_embed()
方法。我一定犯了一些愚蠢的错误,但我很沮丧。
答案 0 :(得分:2)
调试起来变得非常困难,因为我没有意识到一件事:嵌入(包括它们的HTML)在重新保存或发布使用它们的Page时并不总是重新生成。仅当其URL更改时,它们才会重新生成。这就是为什么从未调用过我的find_embed()
方法的原因;因为我只是重新发布页面,而没有更改嵌入中使用的URL。
一旦意识到这一点,我想做的解决方案就很简短。
在我的settings.py
中:
from wagtail.embeds.oembed_providers import youtube
WAGTAILEMBEDS_FINDERS = [
{
'class': 'myapp.embeds.finders.oembed.YouTubeOEmbedFinder',
'providers': [youtube],
},
{
# Handles all other oEmbed providers the default way
'class': 'wagtail.embeds.finders.oembed',
},
]
然后在myapp/embeds/finders/oembed.py
中输入:
from wagtail.embeds.finders.oembed import OEmbedFinder
class YouTubeOEmbedFinder(OEmbedFinder):
"""
Ensures that all YouTube embeds use the youtube-nocookie.com domain
instead of youtube.com.
"""
def find_embed(self, url, max_width=None):
embed = super().find_embed(url, max_width)
embed['html'] = embed['html'].replace(
'youtube.com/embed',
'youtube-nocookie.com/embed')
return embed
然后只是更改我所有现有YouTube嵌入的URL (例如,在其URL末尾添加?v=2
)并重新发布页面的情况。< / p>