I try to render a StreamField of a child page in a Page. I don't manage to render the different StructField within the StreamField. Here is my code
class DefinitionPage(Page):
body = StreamField([
('definition', blocks.StructBlock([
('heading', blocks.CharBlock(label='Titre')),
('paragraph', blocks.RichTextBlock(label='Paragraphe')),
]))
])
content_panels = Page.content_panels + [
StreamFieldPanel('body'),
]
my template. (DefinitionPage is a child of this page.)
{% for post in page.get_children %}
<h2><a href="{% pageurl post %}">{{ post.title }}</a></h2>
{% for block in post.body %}
{% include_block block %}
{% endfor %}
{% endfor %}
post.title is ok but it's like there is no block in post.body. I tried so many things and {% include_block block %} is certainly wrong. I also tried to add a custom template for the StructBlock without success.
How can I do ? I am using Django 2.0 and wagtail 2.0 (I'm new to wagtail but I read the doc) Best regards
Thank you I changed my code In the parent PageModel I added:
And now in my template:
I also created a custom template for my definition (simple it's just en test):
Thanks a lot
Just last questions: Is there a better practice ? .specific in template or in a custom get_context() ? Is it a good practice to add custom template to StructBlock ?
You need to use
page.get_children.specific
-get_children
only returns the basicPage
information common to all page types, which doesn't include thebody
field inDefinitionPage
.