LoginSignup
0
0

More than 3 years have passed since last update.

WagtailのSubBlockを使ってみました

Last updated at Posted at 2019-10-22

In addition to the basic block types above, it is possible to define new block types made up of sub-blocks: for example, a ‘person’ block consisting of sub-blocks for first name, surname and image, or a ‘carousel’ block consisting of an unlimited number of image blocks. These structures can be nested to any depth, making it possible to have a structure containing a list, or a list of structures.

...サブブロックを使えば新しいブロックタイプをつくることが可能です。例えば「姓」ブロック、「名」ブロック、「画像」ブロックから「人物」ブロックを作れます。無数の画像から「カルーセル」ブロックをつくることもできます。これらの構造はどんな深さにもネスト(入れ子に)でき、リストや子構造のリストを持つこともできます。

私はbootstrapのjumbotronを作りたかったので、paragraphとimageを持つJumbotronBlockをつくりました

blog/models.py
...
class JumbotronBlock(blocks.StructBlock):
    paragraph = blocks.RichTextBlock()
    image = ImageChooserBlock()

    class Meta:
        template = 'blog/blocks/jumbotron.html'
...
blog/templates/blog/blocks/jumbotron.html
{% load wagtailcore_tags wagtailimages_tags %}

{% image value.image original as img %}
<div class="jumbotron jumbotron-fluid" style="background-image: url('{{ img.url }}');">
  <div class="container">
    {% include_block value.paragraph %}
  </div>
</div>
blog/models.py
...
class JumbotronPage(Page):
    body = StreamField([
        ('jumbotron', JumbotronBlock()),
    ])

    content_panels = Page.content_panels + [
        StreamFieldPanel('body'),
    ]
...

wagtail_jumbotron.png

0
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
0
0