【问题标题】:Top-level StreamBlock within StreamField doesn't render in templateStreamField 中的顶级 StreamBlock 不在模板中呈现
【发布时间】:2020-03-14 01:59:20
【问题描述】:

我正在尝试使用像文档here 部分中的最后一个 sn-p 一样的 StreamBlock,作为 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 很陌生,所以希望这只是一个简单/概念性的错误。任何帮助表示赞赏。提前致谢。

【问题讨论】:

    标签: python django django-templates wagtail wagtail-streamfield


    【解决方案1】:

    在我的情况下,这不起作用,因为在管理员中创建的块仍处于草稿状态,未发布 (facepalm)。一旦发布,它就会完美运行。

    【讨论】:

      猜你喜欢
      • 2017-07-10
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 2018-10-08
      相关资源
      最近更新 更多