如何将包含图像的帖子从 Wordpress 导入到 Wagtail 2(Draftail 编辑器)?

How to import posts from Wordpress to Wagtail 2 (Draftail editor) including images?

我正在尝试将帖子从 Wordpress 导入 Wagtail,包括图片。

我知道 Draftail 编辑器使用专用标签保存图像,例如:

<embed alt="fonctionnement-pim-schema-640.png" embedtype="image" format="fullwidth" id="412" />

当我从 Wordpress 导入帖子时,我将 img 标签转换为嵌入标签。下面是我的代码片段:

resp = requests.get(img["src"], stream=True)
if resp.status_code != requests.codes.ok:
    print("Unable to import " + img["src"])
    continue
fp = BytesIO()
fp.write(resp.content)
image = Image(title=file_name, width=width, height=height)
image.file.save(file_name, File(fp))
image.save()
try:
    embed_id = image.get_rendition("original").id
    embed_alt = image.get_rendition("original").alt
    new_tag = soup.new_tag('embed', alt=f'{embed_alt}', embedtype="image", format="fullwidth", id=f'{embed_id}')
    img.replace_with(new_tag)

似乎有效。当我检查数据库时,所有 img 标签都替换为格式正确的嵌入标签,并且所有图像都下载到媒体文件夹。

不幸的是,当我查看管理区域时。嵌入标签存在但无法识别图像:

当我在导入脚本中使用通用嵌入标签时(我的意思是不使用“格式”但对所有图像使用相同的嵌入代码)导入运行良好(包括在 Draftail 中)。 “格式”或“f 字符串”会引入错误吗?

如有任何帮助,我们将不胜感激!

提前致谢。

我找到了导致问题的原因。

我正在使用再现图像来获取 ID:

embed_id = image.get_rendition("original").id

但是演绎是针对模板的。在 Draftail 中,我们需要使用图像对象 ID 而不是再现图像 ID:

embed_id = image.id

对于 alt 文本,您可以从初始内容中选择内容。