0

I've got a snippet which has an HTML editor.

@register_snippet
class GalleryCategory(models.Model):
    text_links = StreamField(
        [('text', DemoStreamBlock())],
        null=True,
        blank=True)
    panels = [
        StreamFieldPanel('text_links'),
    ]

    def __str__(self):
        return self.name

When I create an instance from this snippet I am rendering it in the template like this:

<strong>
     {% include_block category.text_links %}
</strong>

However, this outputs:

 <strong>
    <div class="block-text">
     <div class="block-aligned_html"><dl>
      <dt>html</dt>
      <dd>Visit our <a href="/some page/" target="_blank">Facebook page</a> for more albums</dd>
      <dt>alignment</dt>
      <dd>normal</dd>
      </dl>
     </div>
    </div>
 </strong>

Where html, alignment and normal are labels in the text editor.

This is the admin page editor

This is the rendered template

Henry Lynx
  • 1,099
  • 1
  • 19
  • 41

1 Answers1

1

What you're seeing is the default front-end HTML representation of a StructBlock. To override this, you should set a template property on the StructBlock definition, as described at: http://docs.wagtail.io/en/v2.0/topics/streamfield.html#template-rendering

gasman
  • 23,691
  • 1
  • 38
  • 56
  • I can't wrap my head around this. When I add class AlignedHTMLBlock(StructBlock): html = RawHTMLBlock() class Meta: icon = "user" template = 'cms/gallery.html' I am getting TypeError: 'class Meta' got invalid attribute(s): template – Henry Lynx Apr 16 '18 at 16:20
  • I also tried form_template since I am using version: 1.13.1 - it crashes with Exception Type: KeyError Exception Value: 'request' – Henry Lynx Apr 16 '18 at 16:34