首页 > 解决方案 > Wagtail StreamField 在插入新行后未在模板中呈现

问题描述

我有一个基于 wagtail 的 CMS,最近以更明智的方式重新编写了它。我编写了一个脚本来将旧内容迁移到这个基于 wagtail 2.3 的新版本(旧版本是 wagtail 1.11)。我已经编写了迁移脚本(以重新构建各种外键等)并且所有内容都已填充并且似乎正在工作,除了 StreamFields 的渲染。

令人沮丧的是,当我切换回 v2 的测试数据库时,它工作正常(内容被渲染) - 我一直在搜索我的数据库以查找两行之间的差异(在 wagtailcore_page 或 blog_blogpostpage 中)并且看不到任何差异。显然我在 wagtail 获取 StreamField 内容的方式中缺少一些东西,任何人都可以告诉我我在迁移中可能错过了什么吗?非常感谢!!

模型.py

class BlogPostPage(Page):  # Individual blog post
    template = 'blog/post_page.html'
    parent_page_types = ['blog.BlogIndexPage']
    show_in_menus_default = True
    author = models.ForeignKey(
        User, on_delete=models.PROTECT, default=1,
    )
    description = models.CharField(
        max_length=300, blank=False,
        help_text="Add a brief (max 300 characters) description for this blog post."
    )

    date = models.DateField(
        "Post date",
        help_text="This date may be displayed on the blog post. "
                  "It is not used to schedule posts to go live at a later date."
    )
    body = StreamField([
        ('heading', blocks.CharBlock(classname="full title")),
        ('paragraph', blocks.RichTextBlock()),
        ('embed', EmbedBlock()),
        ('image', ImageChooserBlock(classname='img-responsive')),
        ('code', CodeBlock(label='Code')),
        ('table', TableBlock(label='Table'))
    ], help_text="Create content by adding new blocks.")

表 blog_blogpostpage 条目:

"page_ptr_id","description","date","body","author_id"
23,"Now including Blog!","2018-12-06","[{""type"": ""paragraph"", ""value"": ""<p>Since the first release we&#x27;ve made some improvements and upgrades...</p>"", ""id"": ""25fe32be-2090-42dd-8e3e-4df53c494227""}]",15

迁移脚本.sh

INSERT INTO "public"."wagtailcore_page"("path","depth","numchild","title","slug","live","has_unpublished_changes","url_path","seo_title","show_in_menus","search_description","go_live_at","expire_at","expired","content_type_id","owner_id","locked","latest_revision_created_at","first_published_at","live_revision_id","last_published_at","draft_title")
        VALUES
        (E'00010002000O0001',4,0,E'Release: version 2',E'release-version-2',TRUE,FALSE,E'/home/blog/release-version-2/',E'',TRUE,E'',NULL,NULL,FALSE,6,15,FALSE,E'2018-12-06 16:58:10.897348+08',E'2018-12-06 16:58:10.926032+08',NULL,E'2018-12-06 16:58:10.926032+08',E'Release: version 2');

    INSERT INTO "public"."blog_blogpostpage"("page_ptr_id","description","date","body","author_id")
        VALUES
        ((SELECT id FROM wagtailcore_page WHERE path='00010002000O0001'),E'Now including Blog!',E'2018-12-06',E'[{"type": "paragraph", "value": "<p>Since the first release we&#x27;ve made some improvements and upgrades...</p>", "id": "25fe32be-2090-42dd-8e3e-4df53c494227"}]',15);

模板.html

{% include_block page.body %}

^^^ page.body 字段没有显示任何内容,但会呈现描述、日期和作者。

标签: wagtailwagtail-streamfield

解决方案


您的数据迁移创建了无效的 JSON:

[{""type"": ""paragraph"", ""value"": ""<p>S ...

应该有单引号:

[{"type": "paragraph", "value": "<p>S ...

推荐阅读