1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Wagtailチュートリアル 9 blocks.pyで新しいフィールドを作る(2)

Last updated at Posted at 2019-08-26

Wagtailチュートリアル 9

<=[8 blocks.pyで新しいフィールドを作る(1)]
(https://qiita.com/tagawahirotaka/items/82e13c046ef189201299)
=>10 カードを表示しよう
##blocks.pyで新しいフィールドを作る(2)

20,22

strams/blocks.py

from wagtail.core import blocks

# 18 TitleAndTextBlockを定義
class TitleAndTextBlock(blocks.StructBlock):

    title = blocks.CharBlock(required=True, help_text="Add your title")
    text = blocks.TextBlock(required=True, help_text="Add additional text")

    class Meta:
        template = "streams/title_and_text_block.html" # 18 テンプレートの場所
        icon = "edit" # 18 admin画面のアイコン
        label = "Title & Text" # 18 admin画面の表示

# 20 追加
class RichtextBlock(blocks.RichTextBlock):

    class Meta:
        template = "streams/richtext_block.html"
        icon = "doc-full"
        label = "Full RichText"

# 22 追加
class SimpleRichtextBlock(blocks.RichTextBlock):

    # 22 field_block.pyからオーバーライド
    def __init__(self, required=True, help_text=None, editor='default', features=None, **kwargs):
        super().__init__(**kwargs)
        self.features = [
            "bold",
            "italic",
            "link",
        ]


    class Meta:
        template = "streams/richtext_block.html"
        icon = "edit"
        label = "Simple RichText"

20,22

flex/models.py

from django.db import models

# 16 18 モデルに必要なものをインポート
from wagtail.admin.edit_handlers import FieldPanel,StreamFieldPanel
from wagtail.core.models import Page
# 18 モデルに必要なものをインポート
from wagtail.core.fields import StreamField

from streams import blocks # 18 streams/blocks.pyをインポート

# 16 モデルを定義
class FlexPage(Page):

    template = "flex/flex_page.html"

    # 18 追加
    content = StreamField(
        [
            ("title_and_text", blocks.TitleAndTextBlock()),# 18 strams/blocks.pyに定義されている
            ("full_richtext", blocks.RichtextBlock()),# 20 追加
            ("simple_richtext", blocks.SimpleRichtextBlock()),# 22 追加
        ],
        null=True,
        blank=True,
    )

    subtitle = models.CharField(max_length=100, null=True, blank=True)

    content_panels = Page.content_panels + [
        FieldPanel("subtitle"),
        StreamFieldPanel("content"),
    ]

    class Meta:
        verbose_name = "Flex Page"
        verbose_name_plural = "Flex Pages"

21

wagtailtutorial/templates/streams/richtext_block.html

{{self}} {# 21 RichtextBlockを表示 #}

20
スクリーンショット 2019-08-27 08.06.06.png

21
スクリーンショット 2019-08-27 08.09.08.png

22
スクリーンショット 2019-08-27 08.25.31.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?