Wagtail 以编程方式发布图像不会反映在 FE 中

问题描述 投票:0回答:1

创建了一个管理命令,将内容从 Headway 应用程序导入到 wagtail。由于 headwayapp 没有任何 api,我将 html 复制到一个单独的文件中并解析它。除了图像之外,一切都工作正常,除非我单击发布到管理仪表板图像中的特定页面,否则图像不会反映。所有其他内容似乎都运行良好。这是管理命令 任何帮助将不胜感激!

from django.core.management.base import BaseCommand
from wagtail.models import Page
from wagtail.images.models import Image
from wagtail.embeds.models import Embed
from home.models import ChangelogPost, Category, Product
from bs4 import BeautifulSoup
from datetime import datetime
import uuid
from io import BytesIO
import requests
import re
from django.core.files.images import ImageFile


class Command(BaseCommand):
    help = "Import HTML content into Wagtail as ChangelogPost objects."

    def handle(self, *args, **kwargs):

        with open("subscription/migrateHTML/migrate.html", "r", encoding='utf-8') as f:
            html_doc = f.read()

        soup = BeautifulSoup(html_doc, 'html.parser')

        content = []
        blog_divs = soup.find_all('div', class_='changelogItem published')

        for div in blog_divs:

            h2 = div.find('h2')
            content_div = div.find('div', class_='content')
            p = content_div.find('p')
            img_tags = content_div.find_all('img')
            img_htmls = [img.prettify() for img in img_tags]

            h3 = content_div.find('h3')
            if h3:
                span_text = h3.text.strip()

            # Get the content html, ignoring the img tag

            to_decompose = []

            for child in p.next_siblings:
                soup = BeautifulSoup(str(child), 'html.parser')
                if soup.find('img') is not None:
                    to_decompose.append(child)

            for child in to_decompose:
                child.decompose()

            # Get the content html, ignoring the span tag
            content_html = ''.join(
                str(sibling) for sibling in p.next_siblings if sibling.name != 'span')

            # include the first p tag in the content
            content_html = str(p) + content_html

            # Extract date_published from time tag
            date_published = div.find(
                'time')['datetime'] if div.find('time') else None

            if date_published:
                # Convert date_published to the correct format
                date_published = datetime.strptime(
                    date_published, "%Y-%m-%dT%H:%M:%SZ").strftime("%Y-%m-%d")

            if h2:
                # Add the span text as the fourth value
                content.append(
                    [h2.text.strip(), content_html, img_htmls, span_text, date_published])

        parent_page = Page.objects.filter(slug="home")[0]
        categories = Category.objects.all()
        product = Product.objects.all().first()

        for index in range(len(content)):

            # Generate a random UUID
            unique_id = uuid.uuid4()
            unique_id_str = str(unique_id).replace('-', '')[:10]

            val = content[index]

            match = [category for category in categories if val[3]
                     == category.name]

            img_arr = []
            if val[2] is not None:

                for item in val[2]:

                    img_src_regex = r'src="([^"]*)"'
                    src = re.search(img_src_regex, item).group(1)

                    if not src.startswith('https://'):
                        src = 'https:'+src

                    http_res = requests.get(src)
                    title = str(uuid.uuid4().int)[:6] + '.jpg'
                    image = Image(title=title, file=ImageFile(
                        BytesIO(http_res.content), name=title))
                    image.save()

                    imgstr = f"""<embed embedtype='image' id="{
                        image.id}" format='fullwidth' alt='happyfox images'/>"""
                    img_arr.append(imgstr)

            if img_arr:
                for im in img_arr:
                    val[1] += im

            page = ChangelogPost(
                title=val[0],
                content=f"""{val[1]}""",
                slug=unique_id_str,
                published_date=val[4],
                categories=match,
                products=[product]
            )

            new_page = parent_page.add_child(instance=page)
            new_page.save_revision().publish()

        self.stdout.write(
            self.style.SUCCESS(
                f"Migration successfully completed"
            )
        )

我尝试使用此命令发布

new_page.save_revision().publish()
,但图像未渲染。理想情况下图像应该呈现。

python django wagtail wagtail-streamfield wagtail-apiv2
1个回答
0
投票

当您的导入脚本将

<embed>
标记写入富文本字段时,需要用双引号(而不是单引号)引用属性,根据 富文本数据格式文档:

        imgstr = f"""<embed embedtype="image" id="{
            image.id}" format="fullwidth" alt="happyfox images" />"""

这是因为富文本处理代码使用正则表达式替换了

<embed>
标签 - 标签的格式必须与 Wagtail 本身使用的格式完全匹配。

© www.soinside.com 2019 - 2024. All rights reserved.