#Wagtailチュートリアル 8
<=7 新しいページを追加しよう
=>9 blocks.pyで新しいフィールドを作る(2)
##blocks.pyで新しいフィールドを作る(1)
python3 manage.py startapp streams
streamsフォルダができる
18
wagtailtutorial/settings/base.py
......
INSTALLED_APPS = [
'home',
'search',
# 16 flexを追加
'flex',
# 18 streamsを追加
'streams',
'wagtail.contrib.forms',
'wagtail.contrib.redirects',
'wagtail.embeds',
'wagtail.sites',
......
18
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に定義されている
],
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"
18
streams/blocks.py
# 18 TitleAndTextBlockを定義
from wagtail.core import blocks
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画面の表示
19
wagtailtutorial/templates/flex/flex_page.html
{% extends "base.html" %}
{% load wagtailcore_tags %}
{% block content %}
<div>
{{self.subtitle}} {# 17 flexページのsubtitleを表示#}
</div>
{% for block in page.content %}
{% include_block block%} {# 19 streams/block.pyのTitleAndTextBlock をループする#}
{% endfor %}
{% endblock %}
19
wagtailtutorial/templates/streams/title_and_text_block.html
{# 19 streams/blocks.pyのtitle,textをループで表示 #}
<div>
<h2>{{self.title}}</h2>
<p>{{self.text}}</h2>
</div>
</hr>