简体   繁体   English

StreamField 中的顶级 StreamBlock 不在模板中呈现

[英]Top-level StreamBlock within StreamField doesn't render in template

I'm attempting to use a StreamBlock like the last snippet in the section of the docs here , as the only argument to a StreamField.我正在尝试使用 StreamBlock 就像此处文档部分中的最后一个片段一样,作为 StreamField 的唯一参数。 It's working perfectly in the Wagtail admin, but when trying to render in the template nothing appears.它在 Wagtail 管理中完美运行,但是当尝试在模板中渲染时什么也没有出现。 I tried using the default template and just include_block, but nothing is rendered.我尝试使用默认模板,只使用 include_block,但没有渲染任何内容。 I also tried using a custom template and include_block, and the custom template is hit, but I haven't been able to render anything useful from there.我还尝试使用自定义模板和 include_block,并且自定义模板被命中,但我无法从那里呈现任何有用的东西。 I reverted to trying to use the default template in the following code.我恢复尝试在以下代码中使用默认模板。

home/models.py:家/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:家/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:主页/模板/主页/home_page.html:

{% extends "base.html" %}
{% load wagtailcore_tags %}

{% block content %}
    test
    {% include_block page.hero_image %}
{% endblock content %}

I'm pretty new to python/django/wagtail, so hoping it's just a simple/conceptual error.我对 python/django/wagtail 很陌生,所以希望这只是一个简单/概念性的错误。 Any help is appreciated.任何帮助表示赞赏。 Thanks in advance.提前致谢。

In my case, this wasn't working because the blocks created in the admin were still in draft state, not published (facepalm).就我而言,这不起作用,因为在管理员中创建的块仍处于草稿状态,未发布(facepalm)。 Once published, this works perfectly.一旦发布,这将完美运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM