[Django]-Wagtail SnippetChooserBlock in Streamfield

6πŸ‘

βœ…

bios here is defined as a list of BioInline values, and so b in your template would be a BioInline value – which has a single property, bio (giving you the actual BioSnippet object). To get the name, you’d therefore have to use: {{ b.bio.name }}.

I don’t think the BioInline object is actually gaining you anything though – you could instead define BioBlock as:

class BioBlock(StructBlock):
    overall_title = CharBlock(required=False)
    bios = ListBlock(SnippetChooserBlock(BioSnippet))

which would make bios a list of BioSnippets – {{ b.name }} would then work as expected.

πŸ‘€gasman

0πŸ‘

Alternatively, you can use self.bios

In blocks.py you have to import the Snippet model (should have this allready):

from thebioapp.models import BioSnippet

And then use this model in the template itself

{% for b in self.bios %}
    {{ b }}

    <hr>
    {{ b.name }}

{% endfor %}

The post is old, but as Wagtail is growing in popularity, I hope this will benefit others!

πŸ‘€moojen

Leave a comment