StreamField 中的顶级 StreamBlock 不在模板中呈现
Top-level StreamBlock within StreamField doesn't render in template
我正在尝试使用 StreamBlock,就像文档 here 部分的最后一个片段一样,作为 StreamField 的唯一参数。它在 Wagtail 管理员中运行完美,但是当尝试在模板中呈现时,什么也没有出现。我尝试使用默认模板,仅使用 include_block,但未呈现任何内容。我还尝试使用自定义模板和 include_block,自定义模板被点击,但我无法从那里呈现任何有用的东西。我恢复为尝试在以下代码中使用默认模板。
home/models.py:
from django.db import models
from wagtail.core.models import Page
from wagtail.core.fields import StreamField
from wagtail.admin.edit_handlers import StreamFieldPanel
from home.blocks import HeroImageStreamBlock
class HomePage(Page):
hero_image = StreamField(HeroImageStreamBlock(
block_counts={
'hero_button': {'max_num': 1},
'is_active': {'max_num': 1},
'is_randomized': {'max_num': 1},
}
)
, blank=True
)
content_panels = Page.content_panels + [
StreamFieldPanel('hero_image'),
]
home/blocks.py:
from wagtail.core import blocks
from wagtail.images.blocks import ImageChooserBlock
class HeroButtonBlock(blocks.StructBlock):
button_text = blocks.CharBlock(required=False)
button_page = blocks.PageChooserBlock(required=False)
button_url = blocks.URLBlock(required=False)
is_active = blocks.BooleanBlock(required=False,
help_text="""When checked, this hero button is active.
NOTE: Only the first active hero button will be displayed.""")
class Meta:
icon = "link"
class HeroTextBlock(blocks.StructBlock):
hero_text = blocks.CharBlock(required=False)
is_active = blocks.BooleanBlock(required=False,
help_text="""When checked, this hero text is active.
NOTE: Only the first active hero text will be displayed.""")
class Meta:
icon = "edit"
class HeroImageBlock(blocks.StructBlock):
"""This is the StructBlock for the hero images."""
background_image = ImageChooserBlock()
is_active = blocks.BooleanBlock(required=False,
help_text="""When checked, this hero image is active.""")
class Meta:
icon = "image"
class HeroImagesListBlock(blocks.ListBlock):
class Meta:
icon = "media"
class HeroImageStreamBlock(blocks.StreamBlock):
"""This is the container StreamBlock for the hero image."""
hero_images = HeroImagesListBlock(HeroImageBlock())
text = HeroTextBlock()
hero_button = HeroButtonBlock()
is_randomized = blocks.BooleanBlock(required=False,
label="Is Randomized?",
help_text="When checked, images will load in random order.")
is_active = blocks.BooleanBlock(required=False,
help_text="""When checked, this hero image is active.
NOTE: Only the first active hero image will be displayed.""")
home/templates/home/home_page.html:
{% extends "base.html" %}
{% load wagtailcore_tags %}
{% block content %}
test
{% include_block page.hero_image %}
{% endblock content %}
我是 python/django/wagtail 的新手,希望这只是一个 simple/conceptual 错误。任何帮助表示赞赏。提前致谢。
就我而言,这不起作用,因为在管理员中创建的块仍处于草稿状态,未发布 (facepalm)。一经发布,效果完美。
我正在尝试使用 StreamBlock,就像文档 here 部分的最后一个片段一样,作为 StreamField 的唯一参数。它在 Wagtail 管理员中运行完美,但是当尝试在模板中呈现时,什么也没有出现。我尝试使用默认模板,仅使用 include_block,但未呈现任何内容。我还尝试使用自定义模板和 include_block,自定义模板被点击,但我无法从那里呈现任何有用的东西。我恢复为尝试在以下代码中使用默认模板。
home/models.py:
from django.db import models
from wagtail.core.models import Page
from wagtail.core.fields import StreamField
from wagtail.admin.edit_handlers import StreamFieldPanel
from home.blocks import HeroImageStreamBlock
class HomePage(Page):
hero_image = StreamField(HeroImageStreamBlock(
block_counts={
'hero_button': {'max_num': 1},
'is_active': {'max_num': 1},
'is_randomized': {'max_num': 1},
}
)
, blank=True
)
content_panels = Page.content_panels + [
StreamFieldPanel('hero_image'),
]
home/blocks.py:
from wagtail.core import blocks
from wagtail.images.blocks import ImageChooserBlock
class HeroButtonBlock(blocks.StructBlock):
button_text = blocks.CharBlock(required=False)
button_page = blocks.PageChooserBlock(required=False)
button_url = blocks.URLBlock(required=False)
is_active = blocks.BooleanBlock(required=False,
help_text="""When checked, this hero button is active.
NOTE: Only the first active hero button will be displayed.""")
class Meta:
icon = "link"
class HeroTextBlock(blocks.StructBlock):
hero_text = blocks.CharBlock(required=False)
is_active = blocks.BooleanBlock(required=False,
help_text="""When checked, this hero text is active.
NOTE: Only the first active hero text will be displayed.""")
class Meta:
icon = "edit"
class HeroImageBlock(blocks.StructBlock):
"""This is the StructBlock for the hero images."""
background_image = ImageChooserBlock()
is_active = blocks.BooleanBlock(required=False,
help_text="""When checked, this hero image is active.""")
class Meta:
icon = "image"
class HeroImagesListBlock(blocks.ListBlock):
class Meta:
icon = "media"
class HeroImageStreamBlock(blocks.StreamBlock):
"""This is the container StreamBlock for the hero image."""
hero_images = HeroImagesListBlock(HeroImageBlock())
text = HeroTextBlock()
hero_button = HeroButtonBlock()
is_randomized = blocks.BooleanBlock(required=False,
label="Is Randomized?",
help_text="When checked, images will load in random order.")
is_active = blocks.BooleanBlock(required=False,
help_text="""When checked, this hero image is active.
NOTE: Only the first active hero image will be displayed.""")
home/templates/home/home_page.html:
{% extends "base.html" %}
{% load wagtailcore_tags %}
{% block content %}
test
{% include_block page.hero_image %}
{% endblock content %}
我是 python/django/wagtail 的新手,希望这只是一个 simple/conceptual 错误。任何帮助表示赞赏。提前致谢。
就我而言,这不起作用,因为在管理员中创建的块仍处于草稿状态,未发布 (facepalm)。一经发布,效果完美。