Supplying StreamField with default value in Wagtail admin interface
Asked Answered
C

2

6

By default, wagtail's StreamField looks something like this in the admin for empty fields, showing the different blocks available for the user: Default view of a StreamField in the wagtail admin

I would however prefer it to contain a block of my choosing by default. For example:

A StreamField control with a single rich text block with content

How would I accomplish this? Setting a default keyword argument for the field didn't seem to work.

Cali answered 6/3, 2019 at 10:5 Comment(5)
Do you mean setting a default value on the RichTextBlock inside the StreamField?Olds
@Olds while that is also useful, I was referring more to setting which block spawns by default, i.e. so it doesn't start out empty with the choices openCali
So what you're looking for is when a user clicks the plus icon on the StreamField, a RichTextBlock is pre-selected, skipping over the banner with other block types? Or, the banner with other block types stays open and a RichTextBlock appears below?Olds
My intention is that a user wouldn't have to click the "plus" icon and select a block at all. The field will come pre-populated with a block, and a user may add whatever other blocks they wish before/after or whateverCali
Can you post the code that you tried when you were setting the default? I believe the post needs a default value in the StreamField if you want its block to appear without someone opening the plus icon.Olds
O
3

When you tried to set a default keyword, did you set it on the RichTextBlock inside the StreamField? According to the docs, all block types accept the following optional keyword arguments: default, label, group, icon, and template. For example:

body = StreamField([
        ('paragraph', blocks.RichTextBlock(default='Add you content here')),
    ])
Olds answered 1/5, 2019 at 23:16 Comment(0)
D
1

You can use the default option:

    navbarLinks = StreamField(
        [
            (
                "navbarLink",
                blocks.StructBlock(
                    [
                        ("linkText", blocks.CharBlock()),
                        ("linkUrl", blocks.CharBlock(required=False)),
                    ]
                ),
            )
        ],
        default=[{"type": "navbarLink", "value": {"linkText": "Home", "linkUrl": ""}}],
    )
Dorsad answered 2/5 at 20:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.