Skip to content

Commit b2c5b5c

Browse files
authored
Add content streamfield and blocks (#8)
* Add whitenoise to dependencies. * Add settings to enable whitenoise. * Add stream block for a link in a menu. * Add stream block for a page header. * Add content StreamField to home page model.
1 parent 7b39a00 commit b2c5b5c

File tree

14 files changed

+108
-43
lines changed

14 files changed

+108
-43
lines changed

app/alloydflanagan/home/blocks/__init__.py

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from wagtail.blocks import StreamBlock, ListBlock
2+
from wagtail.images.blocks import ImageChooserBlock
3+
from alloydflanagan.home.blocks.menu_link import MenuLinkBlock
4+
5+
6+
class HeaderBlock(StreamBlock):
7+
tabs = ListBlock(MenuLinkBlock())
8+
banner = ImageChooserBlock(required=False)
9+
class Meta:
10+
template = 'home/blocks/header.html'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from wagtail.blocks import StructBlock, CharBlock, PageChooserBlock
2+
3+
4+
class MenuLinkBlock(StructBlock):
5+
title = CharBlock(required=False)
6+
target = PageChooserBlock(required=False)
7+
8+
class Meta:
9+
template = 'home/templates/home/blocks/menu_link.html'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Generated by Django 4.2.21 on 2025-06-29 16:17
2+
3+
from django.db import migrations
4+
import wagtail.blocks
5+
import wagtail.fields
6+
import wagtail.images.blocks
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
dependencies = [
12+
('home', '0004_homepage_resume'),
13+
]
14+
15+
operations = [
16+
migrations.AddField(
17+
model_name='homepage',
18+
name='content',
19+
field=wagtail.fields.StreamField([('text', wagtail.blocks.RichTextBlock()), ('image', wagtail.images.blocks.ImageChooserBlock()), ('header', wagtail.blocks.StreamBlock([('tabs', wagtail.blocks.ListBlock(wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock(required=False)), ('target', wagtail.blocks.PageChooserBlock(required=False))]))), ('banner', wagtail.images.blocks.ImageChooserBlock(required=False))]))], blank=True, use_json_field=True),
20+
),
21+
]

app/alloydflanagan/home/models.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
from django.db.models import FileField
22

33
from wagtail.admin.panels import FieldPanel
4+
from wagtail.blocks import RichTextBlock
5+
from wagtail.images.blocks import ImageChooserBlock
46
from wagtail.models import Page
5-
from wagtail.fields import RichTextField
7+
from wagtail.fields import RichTextField, StreamField
8+
9+
from alloydflanagan.home.blocks.header import HeaderBlock
10+
611

712
class HomePage(Page):
813
intro = RichTextField()
914
# may move resume to AboutPage model if/when we have one
1015
resume = FileField(blank=True)
16+
content = StreamField([
17+
('text', RichTextBlock()), #probably will use custom type later
18+
('image', ImageChooserBlock()),
19+
('header', HeaderBlock()),
20+
], use_json_field=True, blank=True)
1121

1222
content_panels = Page.content_panels + [
1323
FieldPanel("intro", classname="full"),
1424
FieldPanel("resume", classname="full"),
25+
FieldPanel("content", classname="full"),
1526
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{% load wagtailcore_tags wagtailimages_tags %}
2+
3+
<header class="masthead mb-auto">
4+
<div class="inner">
5+
<div>
6+
<h3 class="masthead-brand">A Lloyd Flanagan</h3>
7+
</div>
8+
<div class="header-banner">
9+
{% image block.banner width-800 %}
10+
</div>
11+
<nav class="nav nav-masthead justify-content-center app-nav">
12+
{% for tab in block.tabs %}
13+
{% include_block tab %}
14+
{% endfor %}
15+
</nav>
16+
</div>
17+
</header>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!-- home/blocks/menu_link.html -->
2+
<span class="nav-link"><a href="{{ block.target }}">{{ block.title }}</a></span>

app/alloydflanagan/home/templates/home/home_page.html

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33

44
{% block body_class %}template-homepage{% endblock %}
55

6-
{% block extra_css %}
7-
{% endblock extra_css %}
8-
9-
{% block header %}
10-
{% include 'components/header.html' %}
11-
{% endblock header %}
12-
136
{% block content %}
14-
<div class="home-intro">
7+
<!-- home_page.html -->
8+
9+
{# include_block page.content #}
10+
<article>
11+
<header>
12+
{{ page.header }}
13+
</header>
14+
<section id="intro">
1515
{{ page.intro|richtext }}
16-
</div>
16+
</section>
17+
<main id="content">
18+
{{ page.content }}
19+
</main>
20+
</article>
1721
{% endblock content %}
18-
19-
{% block footer %}
20-
{% include 'components/footer.html' %}
21-
{% endblock footer %}

app/alloydflanagan/settings/base.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@
4949
]
5050

5151
MIDDLEWARE = [
52+
"django.middleware.security.SecurityMiddleware",
53+
"whitenoise.middleware.WhiteNoiseMiddleware",
5254
"django.contrib.sessions.middleware.SessionMiddleware",
5355
"django.middleware.common.CommonMiddleware",
5456
"django.middleware.csrf.CsrfViewMiddleware",
5557
"django.contrib.auth.middleware.AuthenticationMiddleware",
5658
"django.contrib.messages.middleware.MessageMiddleware",
5759
"django.middleware.clickjacking.XFrameOptionsMiddleware",
58-
"django.middleware.security.SecurityMiddleware",
5960
"wagtail.contrib.redirects.middleware.RedirectMiddleware",
6061
]
6162

@@ -132,12 +133,19 @@
132133
# ManifestStaticFilesStorage is recommended in production, to prevent outdated
133134
# JavaScript / CSS assets being served from cache (e.g. after a Wagtail upgrade).
134135
# See https://docs.djangoproject.com/en/3.1/ref/contrib/staticfiles/#manifeststaticfilesstorage
135-
STATICFILES_STORAGE = "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
136+
# STATICFILES_STORAGE = "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
136137
# this should contain any static files that aren't specific to an app
137138
STATICFILES_DIRS = [os.path.join(PROJECT_DIR, "static")]
138139
STATIC_ROOT = os.path.join(BASE_DIR, "static")
139140
STATIC_URL = "/static/"
140-
141+
STORAGES = {
142+
"staticfiles": {
143+
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
144+
},
145+
"default": {
146+
"BACKEND": "django.core.files.storage.FileSystemStorage",
147+
},
148+
}
141149
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
142150
MEDIA_URL = "/media/"
143151

app/alloydflanagan/templates/base.html

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,13 @@
4040

4141
<div id="app">
4242
<div class="cover-container d-flex w-100 h-100 p-3 mx-auto flex-column">
43-
{% block header %}
44-
{% endblock header %}
4543
{% block content %}
4644
{% endblock content %}
47-
{% block footer %}
48-
{% endblock footer %}
4945
</div>
5046
</div>
5147

5248
{# Global javascript #}
53-
<script type="text/javascript" src="{% static 'main.js' %}"></script>
49+
{# <script type="text/javascript" src="{% static 'main.js' %}"></script> #}
5450

5551
{% block extra_js %}
5652

0 commit comments

Comments
 (0)