【问题标题】:Wagtail StreamField not rendering in template after INSERT new rowWagtail StreamField 在插入新行后未在模板中呈现
【发布时间】:2019-05-08 01:07:39
【问题描述】:

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

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

models.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.")

table 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

migration_script.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 字段没有显示任何内容,但会呈现描述、日期和作者。

【问题讨论】:

  • 当您打开外壳并检查字段时会发生什么?例如:python manage.py shellfrom wagtail.core.models import PagePage.objects.get(pk=some_id).specific.body.stream_data 它应该显示一个字典列表。
  • >>> Page.objects.get(pk=127).specific.description '现在包括博客!' >>> Page.objects.get(pk=127).specific.body [] >>> Page.objects.get(pk=127).specific.body.stream_data []
  • 一个空列表! blog_blogpostpage 的 db 列正文包含内容... [{""type"": ""paragraph"", ""value"": ""

    自第一个版本以来,我们进行了一些改进和升级...

    "", ""id"": ""25fe32be-2090-42dd-8e3e-4df53c494227""}]
  • 显然我错过了用户定义的页面模型和 wagtailcore_page 之间的一些基本关系......或其他一些东西:(
  • 这就是多表继承的工作原理。 docs.djangoproject.com/en/2.1/topics/db/models/….

标签: wagtail wagtail-streamfield


【解决方案1】:

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

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

应该有单引号:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    • 2019-01-21
    • 2016-10-29
    • 2019-10-27
    • 2016-08-31
    • 2020-06-15
    • 2012-02-03
    相关资源
    最近更新 更多